#include <stdio.h>
#include<stdlib.h>
typedef int ElementType;
void MSort(ElementType A[],ElementType TmpArry[],int Left,int Right);
void MergeSort(ElementType A[],int N);
void Merge(ElementType A[],ElementType[],int Lpos,int Rpos,int RightEnd);
int main(void) //归并排序
{
int a[10] = {12,33,45,84,6,3,49,51,2,};
int i;
MergeSort(a,10);
for(i = 0; i < 10; i++)
printf("%-4d",a[i]);
printf("\nHello World!\n");
return 0;
}
void MSort(ElementType A[],ElementType TmpArry[],int Left,int Right) //递归排序
{
int Center;
if(Left < Right) //请数列分成两部分,分别排序
{
Center = (Left + Right) / 2;
MSort(A,TmpArry,Left,Center);
MSort(A,TmpArry,Center + 1,Right);
Merge(A,TmpArry,Left,Center + 1,Right); //合并两部分
}
}
void MergeSort(ElementType A[],int N) //排序驱动程序
{
ElementType *TmpArray;
TmpArray = (ElementType*)malloc(N * sizeof(ElementType)); //分配第三个数列,用来合并
if(TmpArray != NULL)
{
MSort(A,TmpArray,0,N - 1);
free(TmpArray);
}
else
printf("No space for tmp array\n");
}
void Merge(ElementType A[],ElementType TmpArray[],int Lpos,int Rpos,int RightEnd)
{ //合并两个有序数列
int i,LeftEnd,NumElements,TmpPos;
LeftEnd = Rpos - 1; //左边数列的最后
TmpPos = Lpos; //左边数列的开始
NumElements = RightEnd - Lpos + 1; //排序的总数
while (Lpos <= LeftEnd && Rpos <= RightEnd) //合并左右数列
{
if(A[Lpos] <= A[Rpos]) //将小的数放在前面
TmpArray[TmpPos++] = A[Lpos++];
else
TmpArray[TmpPos++] = A[Rpos++];
}
while(Lpos <= LeftEnd) //处理数列中剩余的数,与下面的只有一个起作用
{
TmpArray[TmpPos++] = A[Lpos++];
}
while(Rpos <= RightEnd)
{
TmpArray[TmpPos++] = A[Rpos++];
}
for( i = 0; i < NumElements; i++,RightEnd--) //将数复制回原来数组
{
A[RightEnd] = TmpArray[RightEnd];
}
}
#include<stdlib.h>
typedef int ElementType;
void MSort(ElementType A[],ElementType TmpArry[],int Left,int Right);
void MergeSort(ElementType A[],int N);
void Merge(ElementType A[],ElementType[],int Lpos,int Rpos,int RightEnd);
int main(void) //归并排序
{
int a[10] = {12,33,45,84,6,3,49,51,2,};
int i;
MergeSort(a,10);
for(i = 0; i < 10; i++)
printf("%-4d",a[i]);
printf("\nHello World!\n");
return 0;
}
void MSort(ElementType A[],ElementType TmpArry[],int Left,int Right) //递归排序
{
int Center;
if(Left < Right) //请数列分成两部分,分别排序
{
Center = (Left + Right) / 2;
MSort(A,TmpArry,Left,Center);
MSort(A,TmpArry,Center + 1,Right);
Merge(A,TmpArry,Left,Center + 1,Right); //合并两部分
}
}
void MergeSort(ElementType A[],int N) //排序驱动程序
{
ElementType *TmpArray;
TmpArray = (ElementType*)malloc(N * sizeof(ElementType)); //分配第三个数列,用来合并
if(TmpArray != NULL)
{
MSort(A,TmpArray,0,N - 1);
free(TmpArray);
}
else
printf("No space for tmp array\n");
}
void Merge(ElementType A[],ElementType TmpArray[],int Lpos,int Rpos,int RightEnd)
{ //合并两个有序数列
int i,LeftEnd,NumElements,TmpPos;
LeftEnd = Rpos - 1; //左边数列的最后
TmpPos = Lpos; //左边数列的开始
NumElements = RightEnd - Lpos + 1; //排序的总数
while (Lpos <= LeftEnd && Rpos <= RightEnd) //合并左右数列
{
if(A[Lpos] <= A[Rpos]) //将小的数放在前面
TmpArray[TmpPos++] = A[Lpos++];
else
TmpArray[TmpPos++] = A[Rpos++];
}
while(Lpos <= LeftEnd) //处理数列中剩余的数,与下面的只有一个起作用
{
TmpArray[TmpPos++] = A[Lpos++];
}
while(Rpos <= RightEnd)
{
TmpArray[TmpPos++] = A[Rpos++];
}
for( i = 0; i < NumElements; i++,RightEnd--) //将数复制回原来数组
{
A[RightEnd] = TmpArray[RightEnd];
}
}