#include #define maxSize 100
int R[maxSize] = { 0 };
int count = 0;
/*********输入数据*********/
void Indata()
{
printf("请输入整数数据(按回车键停止输入):");
int i = 0;
char ch;
while (1)
{
scanf_s("%d", &R[i]);
i++;
count++;
ch = getchar();
if ('\n' == ch)
break;
}
}
/*********将前后相邻的两个有序表归并成一个有序表******/
void Merge(int r[],int low,int mid,int high)
{
int i, j, k;
int n1 = mid - low + 1;
int n2 = high - mid;
int L[maxSize],M[maxSize];
for (i = 0; i < n1; i++)
L[i] = r[low + i];
for (j = 0; j < n2; j++)
M[j] = r[mid + 1 + j];
i = 0; j = 0; k = low;
while (i < n1 && j < n2)
{
if (L[i] <= M[j])
r[k] = L[i++];
else
r[k] = M[j++];
k++;
}
while (i < n1)
r[k++] = L[i++];
while (j < n2)
r[k++] = M[j++];
}
void MergeSort(int r[],int low,int high)
{
if (low < high)
{
int mid = (low + high) / 2; //从中间划分两个子序列
MergeSort(r, low, mid);
MergeSort(r, mid + 1, high);
Merge(r, low, mid, high);
}
}
void Print(int r[],int n)
{
printf("归并排序后序列为:");
for (int i = 0; i < n; i++) {
printf("%d ", r[i]);
}
}
void main()
{
Indata();
MergeSort(R, 0, count - 1);
Print(R, count - 1);
system("pause");
}