直接插入排序,快速排序,冒泡排序,希尔排序,简单选择排序--算法



#include
#include
#include

//直接插入排序  稳定排序 时间复杂度 o(n*n) 

void zhi_jie_cha_ru(int *a,int n){
	int temp;
	for(int i=1;i<n;i++){
		temp=a[i];
		int j;
		for(j=i-1;j>=0&&a[j]>temp;j--){
			a[j+1]=a[j];
			//前边的[j+1]=1,后边的【j】=0,然后执行j--    因为j现在为-1  <0  所以跳出循环,执行下边的    a[j+1]=temp
		}
		a[j+1]=temp;
	}
}


//快速排序  不稳定   时间复杂度o(nlog2n) 

int zi_pai(int *a,int low,int high){
	int temp=a[low];
	int i,j;
	while(low<high){
		while(low<high && a[high]>=temp){
			high--;
		}
		a[low] = a[high];
		while(low<high && a[low<=temp]){
			low++;
		}
		a[high] = a[low];
		
	}
	a[low] = temp;
	return low;
}

void zhu_pai(int *a,int low, int high){
	int middle;
	
	if(low<high){
		middle = zi_pai(a,low,high);
		
		zhu_pai(a,low,middle-1);
		zhu_pai(a,middle+1,high);
	}
}
 
 
 
 //希尔排序   时间复杂度 o(n3/2)  不稳定 
void xi_er(int *a,int n){
	int bu_chang=n/2;
	int temp;
	for(int d=bu_chang;d>=1;d=d/2){
		for(int i=d;i<n;i++){
			temp=a[i];
			int j;
			for(j=i-d;j>=0 && a[j]>temp; j=j-d){
				a[j+d] = a[j];
			}
			a[j+d] = temp;
		}
	}
}
 
 
 //冒泡排序  稳定排序     时间复杂度o(n*n)
 void mao(int *a,int n){
 	int flag=0;
 	for(int i=0;i<n-1;i++){
 		flag=0;
 		for(int j=0;j<n-1;j++){
 			if(a[j]>a[j+1]){
			 
 			int temp = a[j];
 			a[j] = a[j+1];
 			a[j+1] = temp;
 			flag = 0;
 			}
		 }
		 if(flag==0){
		 	break;
		 }
	 }
 }
 
 
 
 //简单选择排序  稳定排序 时间复杂度o(n*n) 
 
 void jian_dan(int *a,int n){
 	int temp;
 	for(int i=0;i<n-1;i++){
 		for(int j=i+1;j<n;j++){
 			if(a[j]<a[i]){
 				temp = a[j];
 				a[j] = a[i];
 				a[i] = temp;
			 }
		 }
	 }
 }
 
 
int main(){
	int a[4]={20,3,9,56};
	
	jian_dan(a,4);//冒泡排序 
	
	for(int i=0;i<4;i++){
		printf("%d\n",a[i]);
	}
	


	return 0;
}



你可能感兴趣的:(直接插入排序,快速排序,冒泡排序,希尔排序,简单选择排序--算法)