1 //Insert Sort2 intA[n];3 for(intI=2; i<=n;i++)4 {5 ints=A[i];6 intj=i-1;7 while(j&&a[j]>A[i])8 {9a[j+1]=A[j];Tenj--; One } Aa[j+1]=s; - } - the - intPartintA[],intLowintHigh ) - { - inttmp=A[low]; + while(low<High ) - { + while(lowtmp) Ahigh--; at if(low<High ) - { -a[low]=A[high]; -low++; - } - while(lowtmp) inlow++; - if(low<High ) to { +a[high]=A[low]; -high--; the } * } $a[low]=tmp;Panax Notoginseng returnLow ; - } the + voidQuick_sort (intA[],intLowintHigh ) A { the intmid; + if(low<High ) - { $Mid= (Low+high)/2; $Mid=Part (a,low,high); -Quick_sort (a,low,mid-1); -Quick_sort (a,mid+1, high); the } - return ;Wuyi } the - /************ Merge ************/ Wu voidMergeintA[],intLowintHigh ) - { About intb1=Low ; $ intMid= (Low+high)/2; - intB2=mid+1; - intlen=high-low+1; - int*b,k=0; AB= (int*) malloc (len*sizeof(int)); + while(b1<=mid&&b2<=High ) the { - if(a[b1]<=A[B2]) $b[k++]=a[b1++]; the Else theb[k++]=a[b2++]; the } the while(b1<=mid) -b[k++]=a[b1++]; in while(b2<=High ) theb[k++]=a[b2++]; the for(intI=0; i<len;i++) Abouta[low++]=B[i]; the Free (b); the } the + voidMerge_sort (intA[],intLowintHigh ) - { the intMid= (Low+high)/2;Bayi if(low<High ) the { the Merge_sort (a,low,mid); -Merge_sort (a,mid+1, high); - merge (A,low,high); the } the return ; the}
Qsort,mergesort, inserting sort