# 归并排序的实现

//将有序数组a[]b[]合并到c[]

void MemeryArray(int a[], int n, int b[], int m, int c[])

{

int i, j, k;

i = j = k = 0;

while (i < n && j < m)

{

if (a[i] < b[j])

c[k++] = a[i++];

else

c[k++] = b[j++];

}

while (i < n)

c[k++] = a[i++];

while (j < m)

c[k++] = b[j++];

}

bool MergeSort(int a[], int n)

{

int *pTempArray = new int[n];

if (p == NULL)

return false;

mergesort(a, 0, n - 1, pTempArray);

return true;

}

void mergesort(int a[], int first, int last, int temp[])

{

if (first < last)

{

int mid = (first + last) / 2;

mergesort(a, first, mid, temp);    //左边有序

mergesort(a, mid + 1, last, temp); //右边有序

mergearray(a, first, mid, last, temp); //再将二个有序数列合并

}

}

//将有二个有序数列a[first...mid]a[mid...last]合并。

void mergearray(int a[], int first, int mid, int last, int temp[])

{

int i = first, j = mid + 1;

int m = mid,   n = last;

int k = 0;

while (i <= m && j <= n)

{

if (a[i] < a[j])

temp[k++] = a[i++];

else

temp[k++] = a[j++];

}

while (i <= m)

temp[k++] = a[i++];

while (j <= n)

temp[k++] = a[j++];

for (i = 0; i < k; i++)

a[first + i] = temp[i];

}

20000个随机数据进行测试：

50000个随机数据进行测试：

posted @ 2011-08-11 13:45  searchDM  阅读(299)  评论(0编辑  收藏  举报