#include <iostream> using namespace std;
Merge sort is not recursive version.
void Sort (int a[], int n,int high) {int k; for (int i = 0; i < i = 2 * n) {int x = i;//001 int y = i + n;//2 2 3 int z = i +
2 * n;//4 if (z>high) {z = high;
for (int j = y; J <= Z; j +) {int temp = a[j]; for (k = j; k > 0; k--) {if (a[k-1]>temp) {a[k]
= A[k-1];
} else {break;
} A[k] = temp;
When merging is required, I choose to use Insert sorting without opening up new space.
}} void grial (int a[],int high) {int n = high/2+1;//terminate position.
1 2 3 for (int i = 1; I <= n; I *= 2) {Sort (A,i,high); int main () {int a[] = {5,4,3,7,1,3,2,0,5,6,7,9,654,5,423,3,4, 5,6,8,523,423,4,5,6,7,8,45,6,53, 423,4,100};
Grial (A,sizeof (a)/sizeof (int)-1);
for (int i = 0; i < sizeof (a)/sizeof (int); i++) {cout << a[i] << "";
} cout << Endl;
return 0; }
#include <iostream> using namespace std;
Recursive version of the merge sort.
void Sort (int a[], int low, int mid, int high) {
int i;
Int J;
int temp;
for (i = mid+1 i <= high; i++) {temp = A[i];
for (j = i; j > low;j--) {if (temp < a[j-1]) {a[j] = a[j-1];
} else {break;
}//Now that we are close to sorting, we can choose to use the Insert sort or bubble sort to increase efficiency//And not open up additional space.
A[J] = temp; } void Sert (int a[],int low,int high) {if
(Low >= high) return;
int mid = (low + high)/2;
Sert (A,low, mid);
Sert (A, mid + 1, high);
Sort (A,low,mid,high); int main () {int a[] = {6,2,1,3,4,-1,2-3,4,5,6,7,532,4,432,-23, -32,1,-3,21,-321,-3,5,6,8,432,5,66,991};
Sert (A, 0,sizeof (a)/sizeof (int)-1);
for (int i = 0; i < sizeof (a)/sizeof (int); i++) {cout << a[i] << "";
} cout << Endl;
return 0; }