归并排序同为分治法,但是与快速排序不同
快速排序更偏向于分,在分的递归过程中的两侧数据交换
而归并排序更偏向于合并,在两半部分合并的递归中的按顺序排序
代码:
root@ubuntu:/mnt/shared/appbox/mgsort# cat mgsort.c #include <stdlib.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <stdio.h> static int compare_int(const void *int1, const void *int2) { if(*(const int *)int1 == *(const int *)int2) return 0; return *(const int *)int1 > *(const int *)int2 ? 1 : -1; } static int merge(void *data, int esize, int i, int j, int k, int (*compare)(const void *key1, const void *key2)) { char *a = data; char *m; int ipos, jpos, mpos; ipos = i; jpos = j + 1; mpos = 0; if((m = (char *)malloc(esize * ((k-i)+1))) == NULL) return -1; while(ipos <= j || jpos <= k) { if(ipos > j)// left no more elements to merge { while(jpos <= k)//merge right { memcpy(&m[mpos * esize], &a[jpos * esize], esize); jpos++; mpos++; } continue; } else if(jpos > k)//right no more elements to merge { while(ipos <= j)// merge left { memcpy(&m[mpos * esize], &a[ipos * esize], esize); ipos++; mpos++; } continue; } //both left and right need merge if(compare(&a[ipos * esize], &a[jpos * esize]) < 0) { memcpy(&m[mpos * esize], &a[ipos * esize], esize); ipos++; mpos++; } else { memcpy(&m[mpos * esize], &a[jpos * esize], esize); jpos++; mpos++; } } memcpy(&a[i * esize], m, esize * ((k-i)+1)); free(m); return 0; } int mgsort(void *data, int size, int esize, int i, int k , int (*compare)(const void *key1, const void *key2)) { int j; if(i < k) { j = (int)(((i + k -1))/2); if(mgsort(data, size, esize, i, j, compare) < 0) return -1; if(mgsort(data, size, esize, j+1, k, compare) < 0) return -1; if(merge(data, esize, i, j, k, compare)< 0) return -1; } return 0; } int main(int argc, char *argv[]) { int ret; int i, j, k; i = 11; j = 12; int array[10]= {1,4,7,2,5,8,3,6,9,10}; printf("\n******************compare_init test**********************\n"); printf("compare_int(&i, &j)=%d\n", compare_int(&i, &j)); printf("compare_int(&j, &i)=%d\n", compare_int(&j, &i)); printf("compare_int(&i, &i)=%d\n", compare_int(&i, &i)); printf("\n******************partition test**********************\n"); mgsort(array, 10, sizeof(int), 0, 9, compare_int); for(i=0; i<10; i++) printf("%d ", array[i]); printf("\n"); return 0; }
root@ubuntu:/mnt/shared/appbox/mgsort# ./mgsort ******************compare_init test********************** compare_int(&i, &j)=-1 compare_int(&j, &i)=1 compare_int(&i, &i)=0 ******************partition test********************** 1 2 3 4 5 6 7 8 9 10 root@ubuntu:/mnt/shared/appbox/mgsort#