排序算法---直接插入、shell、快速

#include 
int a[10] = {7,4,1,0,2,5,8,9,6,3};

void print_a()
{
	printf("\n");
	int i ;
	for(i = 0;i < sizeof(a) / sizeof(a[0]);i++){
		printf("%d ",a[i]);
	}
	printf("\n"); 
}
void InsertSort_Down(int* arr, int length){
     int i;
	//假定第0个元素是有序表,从第1个元素开始往有序表中插入数据
	for (i = 1; i < length; i++){
		int temp = arr[i];
		int j;
		for (j = i - 1; j >= 0 && arr[j] < temp; j--){
			arr[j + 1] = arr[j];//往前挪
		}
		arr[j + 1] = temp;
	}
	return;
}

void insert_sort()
{
	int i = 0, j = 0,tmp;
	int len = sizeof(a) / sizeof(a[0]);
	for(i = 1;i < len;i++){
		tmp = a[i];

		for(j = i-1 ;j >= 0 && a[j] > tmp;j--){
			a[j+1] = a[j];
		}
		a[j+1] = tmp;
	}
}

void shell_sort()
{
	int i,j,gap,tmp;
	int len = sizeof(a) / sizeof(a[0]);
	
	for(gap = len/2;gap > 0;gap/=2){
		printf("gap:%d\n",gap);
		for(i = gap;i < len;i++){
			tmp = a[i];
	
			for(j = i-gap ;j >= 0 && a[j] > tmp;j-=gap){
				a[j+gap] = a[j];
			}
			a[j+gap] = tmp;
		}
	}
	
}
void quicksort(int left,int right)
{
	int i ,j ,tmp,t;
	if(left > right){
		return ;
	}	
	
	tmp = a[left];
	i = left;
	j = right;
	
	while(i!=j){
		
		while(a[j] >= tmp && i < j){
			j--;
		}
		
		while(a[i] <= tmp && i < j){
			i++;
		}
		
		if(i

 

你可能感兴趣的:(日常总结)