#include<iostream>
#include<vector>
using namespace std;
void mergesort(int Arr[], int l, int r);
void merge(int Arr[], int l, int m, int r);
int main()
{
int arr[10] = { 10,9,4,7,8,5,6,3,2,1 };
for (int i = 0; i < 10; i++)
cout << arr[i] << endl;
mergesort(arr, 0, 9);
cout << "排序后:" << endl;
for (int i = 0; i < 10; i++)
cout << arr[i] << endl;
return 0;
}
void mergesort(int Arr[], int l, int r)
{
//int m;
if (l < r)
{
int m = (l + r) / 2;
mergesort(Arr, l, m);
mergesort(Arr, m + 1, r);
merge(Arr, l, m, r);
}
}
void merge(int Arr[], int l, int m, int r)
{
int i = l, j = m + 1;
int mid = m, end = r;
int k = 0;
int temp[10];
while (i <= mid && j <= end)
{
if (Arr[i] <=Arr[j])
temp[k++] = Arr[i++];
else
temp[k++] = Arr[j++];
}
while (i <= mid)
temp[k++] = Arr[i++];
while (j <= end)
temp[k++] = Arr[j++];
for (int i = 0; i < k; i++)
Arr[l + i] = temp[i];//排好序后代替当前的数组
}