####希爾排序算法
void shellsort(int A[],int n); void shellsort(int A[],int n){ int d,i,x,j; d = n /2 ; while(d>=1){ for(i =d ;i<n;i++){ x = A[i]; j = i-d; while (j>=0&&A[j]>x) { A[j+d] = A[j]; j = j -d; } A[j+d] = x; } d = d /2; } } int main(int argc, const char * argv[]) { // insert code here... int A[9] = {12,34,54,2,1,4,3,5,6}; shellsort(A, 9); for(int i = 0;i<9;i++){ printf("%d",A[i]); } return 0; }
####堆排序法shell
void HeapAdjust(int A[] , int s ,int n){ int j,t; while(2*s+1<n){ j = 2*s +1; if((j+1)<n){ if(A[j]<A[j+1]) j++; } if(A[s]<A[j]){ t = A[s]; A[s] = A[j]; A[j] = t; s = j; }else break; } } void HeapSort(int A[],int n){ int t ,i; int j; for(i = n / 2 -1;i >= 0; --i){ HeapAdjust(A,i,n); } for(i = n - 1;i > 0;i--){ t = A[0]; A[0] = A[i]; A[i] = t; HeapAdjust(A,0,i); } }
####桶排序code
//得到桶排序的MAX值 int GetMaxVal(int * arr,int len){ int maxVal = arr[0]; for(int i = 1; i<len;i++){ if(arr[i]>maxVal){ maxVal = arr[i]; } } return maxVal; } void BucketSort(int * arr,int len){ int tempArrlen = GetMaxVal(arr, len)+1; int tempArr[tempArrlen]; //得到空桶大小 int i,j; for(i =0;i<tempArrlen;i++){ //空桶初始化 tempArr[i] = 0; } for(i = 0;i<len;i++){ tempArr[arr[i]]++; } for(i=0,j=0;i<tempArrlen;i++){ while(tempArr[i]!=0){ arr[j] = i; j++; tempArr[i]--; } } } int main(int argc, const char * argv[]) { // insert code here... int A[10] = {3,123,4,4,1,23,534,21,12,4}; BucketSort(A, 10); for(int i =0;i<10;i++){ printf("%d,",A[i]); } printf("Hello, World!\n"); return 0; }