1 void qsort(int l, int r) { 2 int i, j, mid, p; 3 i=l; j=r; 4 mid=a[(l+r)/2]; 5 do { 6 while (a[i]<mid) i++; 7 while (a[j]>mid) j--; 8 if (i<=j) { 9 p=a[i]; a[i]=a[j]; a[j]=p; 10 i++; j--; 11 } 12 }while(i<=j); 13 if (l<j) qsort(l, j); 14 if (i<r) qsort(i, r); 15 }
1 void mergesort(int s, int t) { 2 int m, i, j, k; 3 if (s==t) return; 4 m=(s+t)/2; 5 mergesort(s, m); 6 mergesort(m+1, t); 7 i=s; 8 j=m+1; 9 k=s; 10 while (i<=m && j<=t) { 11 if (a[i]<=a[j]) { r[k]=a[i]; i++; k++;} 12 else { r[k]=a[j]; j++; k++;} 13 } 14 while (i<=m) { r[k]=a[i]; i++; k++;} 15 while (j<=t) { r[k]=a[j]; j++; k++;} 16 for (i=s; i<=t; i++) a[i]=r[i]; 17 }