归并排序是对两个已经排序的数组合并在一起。
下面使用迭代的方式实现归并排序
#define _SCL_SECURE_NO_WARNINGS
#include
#include
using namespace std;
template<class T>
void Merge(T *initList, T *mergedList, const int l, const int m, const int n){
int i1, i2, iResult;
for (i1 = l, i2 = m + 1, iResult = l; i1 <= m && i2 <= n; iResult++){
if (initList[i1] <= initList[i2]){
mergedList[iResult] = initList[i1];
i1++;
}
else{
mergedList[iResult] = initList[i2];
i2++;
}
}
copy(initList + i1, initList + m + 1, mergedList + iResult);
copy(initList + i2, initList + n + 1, mergedList + iResult);
}
int main(){
int a[] = { 0, 23, 47, 81, 95, 7, 14, 39, 55, 62, 74 };
int b[11] = { 0 };
Merge(a, b, 1, 4, 10);
for (int i = 1; i < 11; ++i){
cout << b[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
下面看一步一步进行归并排序
#define _SCL_SECURE_NO_WARNINGS
#include
#include
using namespace std;
template<class T>
void Merge(T *initList, T *mergedList, const int l, const int m, const int n){
int i1, i2, iResult;
for (i1 = l, i2 = m + 1, iResult = l; i1 <= m && i2 <= n; iResult++){
if (initList[i1] <= initList[i2]){
mergedList[iResult] = initList[i1];
i1++;
}
else{
mergedList[iResult] = initList[i2];
i2++;
}
}
copy(initList + i1, initList + m + 1, mergedList + iResult);
copy(initList + i2, initList + n + 1, mergedList + iResult);
}
template<class T>
void MergePass(T *initList, T *resultList, const int n, const int s){
int i;
for (i = 1; i <= n - 2 * s + 1; i += 2 * s){
Merge(initList, resultList, i, i + s - 1, i + 2 * s - 1);
}
if ((i + s - 1) < n){
Merge(initList, resultList, i, i + s - 1, n);
}
else{
copy(initList + i, initList + n + 1, resultList + i);
}
}
int main(){
/*int a[] = { 0, 23, 47, 81, 95, 7, 14, 39, 55, 62, 74 };
int b[11] = { 0 };
Merge(a, b, 1, 4, 10);
for (int i = 1; i < 11; ++i){
cout << b[i] << " ";
}
cout << endl;*/
int m[] = { 0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 };
int n[11] = { 0 };
MergePass(m, n, 10, 1);
for (int i = 1; i < 11; i++)
cout << n[i] << " ";
cout << endl;
MergePass(n, m, 10, 2);
for (int i = 1; i < 11; i++)
cout << m[i] << " ";
cout << endl;
MergePass(m, n, 10, 4);
for (int i = 1; i < 11; i++)
cout << n[i] << " ";
cout << endl;
MergePass(n, m, 10, 8);
for (int i = 1; i < 11; i++)
cout << m[i] << " ";
cout << endl;
system("pause");
return 0;
}
下面看整体的归并排序:
#define _SCL_SECURE_NO_WARNINGS
#include
#include
using namespace std;
template<class T>
void Merge(T *initList, T *mergedList, const int l, const int m, const int n){
int i1, i2, iResult;
for (i1 = l, i2 = m + 1, iResult = l; i1 <= m && i2 <= n; iResult++){
if (initList[i1] <= initList[i2]){
mergedList[iResult] = initList[i1];
i1++;
}
else{
mergedList[iResult] = initList[i2];
i2++;
}
}
copy(initList + i1, initList + m + 1, mergedList + iResult);
copy(initList + i2, initList + n + 1, mergedList + iResult);
}
template<class T>
void MergePass(T *initList, T *resultList, const int n, const int s){
int i;
for (i = 1; i <= n - 2 * s + 1; i += 2 * s){
Merge(initList, resultList, i, i + s - 1, i + 2 * s - 1);
}
if ((i + s - 1) < n){
Merge(initList, resultList, i, i + s - 1, n);
}
else{
copy(initList + i, initList + n + 1, resultList + i);
}
}
template<class T>
void MergeSort(T *a, const int n){
T *tempList = new int[n + 1];
for (int l = 1; l < n; l *= 2){
MergePass(a, tempList, n, l);
l *= 2;
MergePass(tempList, a, n, l);
}
delete[]tempList;//删除临时的数组,防止内存泄露。
}
int main(){
/*int a[] = { 0, 23, 47, 81, 95, 7, 14, 39, 55, 62, 74 };
int b[11] = { 0 };
Merge(a, b, 1, 4, 10);
for (int i = 1; i < 11; ++i){
cout << b[i] << " ";
}
cout << endl;*/
int m[] = { 0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 };
int n[11] = { 0 };
MergePass(m, n, 10, 1);
for (int i = 1; i < 11; i++)
cout << n[i] << " ";
cout << endl;
MergePass(n, m, 10, 2);
for (int i = 1; i < 11; i++)
cout << m[i] << " ";
cout << endl;
MergePass(m, n, 10, 4);
for (int i = 1; i < 11; i++)
cout << n[i] << " ";
cout << endl;
MergePass(n, m, 10, 8);
for (int i = 1; i < 11; i++)
cout << m[i] << " ";
cout << endl;
cout << "上面是中间数据测试" << endl;
cout << "下面是MergeSort...:" << endl;
int x[] = { 0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 };
MergeSort(x, 10);
for (int i = 1; i < 11; i++)
cout << x[i] << " ";
cout << endl;
system("pause");
return 0;
}
总结:归并排序的速度和快速排序一样,使用三个函数写成一个归并排序,唯一的缺点是需要使用两倍的内存, 如果要排序1M的数据,就要使用2M的内存,因为使用了临时数组。但是现在电脑内存都比较大,所以这个问题也不算严重。