/**
* 归并排序 */ #include<stdio.h> #include<stdlib.h> void changesort(int * array, int lower, int hight, int mid); void megersort(int * array, int lower, int hight) { if( lower >= hight){ return; } int mid = ((hight-lower)/2)+lower; megersort(array,lower, mid); megersort(array,mid+1,hight); changesort(array,lower,hight,mid); } void changesort(int * array, int lower, int hight, int mid) { int arraySize = (hight - lower)+1; int left = lower, right = hight, center = mid+1; int *tmp = (int *)calloc(sizeof(int), arraySize); int i =0; while( i < arraySize && lower <=mid && center<= hight) { if( array[lower] < array[center]){tmp[i] = array[lower]; i ++; lower ++; continue;} tmp[i] = array[center]; i ++; center ++; } while(lower<= mid){ tmp[i] = array[lower]; i++; lower++;} while(center <= hight){ tmp[i] = array[center]; i++; center++;} i =0; while(left <= right){ array[left] = tmp[i]; left ++;i++;} free(tmp); } int main() { int array[] = {8,5,0,10,8,9,4,2,1}; int size = sizeof(array)/sizeof(int); printf("size is: %d\n",size); megersort(array,0, size-1); int i=0; for(; i < size; i++) { printf("%d\t",array[i]); } printf("\n"); return 0;}
1 /** 2 * 冒泡排序 3 */ 4 #include<stdio.h> 5 6 int main() 7 { 8 int a[] = {2,4,62,567,78,12,23,3}; 9 int len = sizeof(a)/sizeof(int); 10 int i=0, j=0, tmp=0 , isc=1; 11 12 for (; i < len ; i++){ 13 14 if(isc ==0) { continue; } 15 isc=0; 16 for(j=0; j < len-i-1; j++) 17 { 18 if(a[j] > a[j+1]){ 19 isc=1; 20 tmp = a[j]; 21 a[j] = a[j+1]; 22 a[j+1] = tmp; 23 } 24 } 25 } 26 27 for(i=0; i < len; i++){printf("%d\t",a[i]);} 28 29 printf("\n"); 30 return 1; 31 32 }