二分归并排序法
问题描述
二分归并排序:对n个不同的数构成的数组A[1…n]进行排序,其中n=2^k
解析
核心代码
int mergearray(int a[], int left, int mid, int right, int temp[])
{
int i = left;
int j = mid + 1;
int m = mid;
int n = right;
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[left + i] = temp[i];
}
int mergesort(int a[], int left, int right, int temp[])
{
if (left < right)
{
int mid = (left + right) / 2;
mergesort(a, left, mid, temp);
mergesort(a, mid + 1, right, temp);
mergearray(a, left, mid, right, temp);
}
}
时间复杂度
T(n)=O(nlog2n)
github源码
https://github.com/1651928813/Pepsi_juice/tree/master/%E4%BD%9C%E4%B8%9A%E5%9B%9B