//时间复杂度 O(N*log2N)
//稳定程度: 稳定
/*
确定分界点,中间位置
两端排序
归并,合二为一
*/#include<iostream>
#include<time.h>
using namespace std;
int tmp[250001];
void Sort(int List[], int l, int r);int main()
{int a[250000];int k, j;// 设置种子srand((unsigned)time(NULL));/* 生成 10 个随机数 */for (k = 0; k < 250000; k++){// 生成实际的随机数j = rand();a[k] = j;}clock_t start_time = clock();Sort(a,0,250000-1);clock_t end_time = clock();//for (int i = 0; i < 200000; i++)//{// cout << a[i] << " ";//}cout << "\n程序段运行时间:" << static_cast<double> (end_time - start_time) / CLOCKS_PER_SEC * 1000 << "ms" << endl;system("pause");
}
void Sort(int List[], int l, int r)
{if (l >= r) return;int mid = l + r >> 1; //取中间数Sort(List, l, mid), Sort(List, mid + 1, r); //左右递归排序int k = 0, i = l, j = mid + 1; //k表示已合并数组中有几个元素,分开两个有序数组while (i <= mid && j <= r) //进行双指针比较if (List[i] <= List[j]) tmp[k++] = List[i++]; else tmp[k++] = List[j++];while (i <= mid) tmp[k++] = List[i++]; //分别处理剩余部分while (j <= r) tmp[k++] = List[j++];for (i = l, j = 0; i <= r; i++, j++) List[i] = tmp[j]; //拷入原空间}