有序子列的归并
简单来说,将一个序列的两个有序子列归并成一个有序子列。
实现起来也不会很难,主要就是有三个指针,每次比较两个序列指针指向的值的大小,然后移入一个到最终序列中,并且进行操作的序列指针分别向后移动,依次进行,直到最后。
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void Merge(int A[], int TmpA[], int L, int R, int RightEnd)
{
int LeftEnd = R - 1;
int Tmp = L;
int NumElements = RightEnd - L + 1;
while(L <= LeftEnd && R <= RightEnd)
{
if (A[L] <= A[R])
{
TmpA[Tmp++] = A[L++];
}
else
{
TmpA[Tmp++] = A[R++];
}
}
while(L <= LeftEnd)
{
TmpA[Tmp++] = A[L++];
}
while(R <= RightEnd)
{
TmpA[Tmp++] = A[R++];
}
for (int i = 0; i < NumElements; i++, RightEnd--)
{
A[RightEnd] = TmpA[RightEnd];
}
}
int main(int argc, char** argv)
{
int A[12] = {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12};
int B[6] = {};
Merge(A, B, 0, 6, 12);
for (int i = 0; i < 12; i++)
{
cout << A[i] << " ";
}
cout << endl;
return 0;
}
递归算法实现有序子列归并
递归算法来实现有序子列的归并是如何实现的呢?
主要是分而治之的思想
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void Merge(int A[], int TmpA[], int L, int R, int RightEnd);
void MSort(int A[], int tmpA[], int L, int RightEnd);
void Merge(int A[], int TmpA[], int L, int R, int RightEnd)
{
int LeftEnd = R - 1;
int Tmp = L;
int NumElements = RightEnd - L + 1;
while(L <= LeftEnd && R <= RightEnd)
{
if (A[L] <= A[R])
{
TmpA[Tmp++] = A[L++];
}
else
{
TmpA[Tmp++] = A[R++];
}
}
while(L <= LeftEnd)
{
TmpA[Tmp++] = A[L++];
}
while(R <= RightEnd)
{
TmpA[Tmp++] = A[R++];
}
for (int i = 0; i < NumElements; i++, RightEnd--)
{
A[RightEnd] = TmpA[RightEnd];
}
}
void MSort(int A[], int TmpA[], int L, int RightEnd)
{
int center;
if (L < RightEnd)
{
center = (L + RightEnd) / 2;
MSort(A, TmpA, L, center);
MSort(A, TmpA, center+1, RightEnd);
Merge(A, TmpA, L, center+1, RightEnd);
}
}
int main(int argc, char** argv)
{
int A[12] = {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12};
int B[6] = {};
Merge(A, B, 0, 6, 11);
for (int i = 0; i < 12; i++)
{
cout << A[i] << " ";
}
cout << endl;
int C[12] = {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12};
MSort(C, B, 0, 11);
for (int i = 0; i < 12; i++)
{
cout << C[i] << " ";
}
cout << endl;
return 0;
}
然而到这里依然没有结束
我们看到上面的递归算法函数需要传进去一大推的参数,这样显然是不够友好的,那么我们如何能改进算法,使得能向之前的排序算法一样友好呢?
我们看下面的代码
#include <iostream>
#include <stdlib.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void Merge(int A[], int TmpA[], int L, int R, int RightEnd);
void MSort(int A[], int tmpA[], int L, int RightEnd);
void Merge_Sort(int A[], int N);
void Merge(int A[], int TmpA[], int L, int R, int RightEnd)
{
int LeftEnd = R - 1;
int Tmp = L;
int NumElements = RightEnd - L + 1;
while(L <= LeftEnd && R <= RightEnd)
{
if (A[L] <= A[R])
{
TmpA[Tmp++] = A[L++];
}
else
{
TmpA[Tmp++] = A[R++];
}
}
while(L <= LeftEnd)
{
TmpA[Tmp++] = A[L++];
}
while(R <= RightEnd)
{
TmpA[Tmp++] = A[R++];
}
for (int i = 0; i < NumElements; i++, RightEnd--)
{
A[RightEnd] = TmpA[RightEnd];
}
}
void MSort(int A[], int TmpA[], int L, int RightEnd)
{
int center;
if (L < RightEnd)
{
center = (L + RightEnd) / 2;
MSort(A, TmpA, L, center);
MSort(A, TmpA, center+1, RightEnd);
Merge(A, TmpA, L, center+1, RightEnd);
}
}
void Merge_Sort(int A[], int N)
{
int *TmpA;
TmpA = (int *)malloc(N);
if (TmpA != NULL)
{
MSort(A, TmpA, 0, N-1);
free(TmpA);
}
else
{
cout << "空间不足" << endl;
}
}
int main(int argc, char** argv)
{
int A[12] = {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12};
int B[6] = {};
Merge(A, B, 0, 6, 11);
for (int i = 0; i < 12; i++)
{
cout << A[i] << " ";
}
cout << endl;
int C[12] = {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12};
MSort(C, B, 0, 11);
for (int i = 0; i < 12; i++)
{
cout << C[i] << " ";
}
cout << endl;
int D[12] = {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12};
Merge_Sort(D, 12);
for (int i = 0; i < 12; i++)
{
cout << D[i] << " ";
}
cout << endl;
return 0;
}