代码
#include<iostream>
using namespace std;void merge(int arr[], int p, int q, int r, int temp[])
{int i = p;int j = q + 1;int k = 0;while (i <= q && j <= r){if (arr[i] < arr[j]){temp[k++] = arr[i++];}else{temp[k++] = arr[j++];}}while (i <= q){temp[k++] = arr[i++];}while (j <= r){temp[k++] = arr[j++];}for (k = 0; k <= (r - p); k++){arr[p + k] = temp[k];}
}
void mergesort(int arr[], int head, int end, int temp[])
{if (head < end){int mid = (head + end) / 2;mergesort(arr, head, mid, temp);mergesort(arr, mid+1, end, temp);merge(arr, head, mid, end, temp);}
}
int main()
{int arr[10] = { 1,3,6,3,99,3,74,11,5,7 };int temp[100] = { 0 };mergesort(arr, 0, 9, temp);for (int i = 0; i < sizeof(arr)/sizeof(int); i++){cout << arr[i] << " ";}return 0;
}