#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 110;
int merge(int a[], int left1, int right1, int l2,int r2) {
int i = left1, j = l2;
int temp[maxn], index = 0;
while (i<=right1 && j<=r2)
{
if (a[i] <= a[j]) {
temp[index++] = a[i++];
}
else {
temp[index++] = a[j++];
}
}
while (i <= right1)
{
temp[index++] = a[i++];
}
while (j <= r2)
{
temp[index++] = a[j++];
}
for (int k = 0; k < index; ++k) {
a[left1+k] = temp[k];
}
return index;//返回长度
}
void mergesort(int A[], int left, int right) {//递归
if (left < right) {
int mid = left + (right - left) / 2;
mergesort(A, left, mid);
mergesort(A, mid + 1, right);
merge(A, left, mid, mid + 1, right);
}
}
void mergesort_2(int A[],int n)//非递归
{
for (int step = 2; step / 2 <= n; step *= 2) {
for (int i = 0; i < n; i += step) {
int mid = i + step / 2 - 1;
if (mid + 1<n)
merge(A, i, mid, mid + 1, min(n, i + step - 1));
}
}
getchar();
}
int main(void) {
int num[maxn] = { 23,34,5,7,5,24,7,13,7,68,954,4,51,9 };
mergesort(num,0, 15);
return 0;
}