#include <stdio.h>
#include <stdlib.h>
void Merge(int arr[], int low, int mid , int high, int temp[]) {
int i, j,k;
for(i = low, j = mid+1,k = 0; i<=mid && j <=high;k++) {
if(arr[i] < arr[j]) temp[k] = arr[i++];
else temp[k] = arr[j++];
}
if(i<=mid) while(i<=mid){temp[k++] = arr[i++];}
else while(j<=high){temp[k++] = arr[j++];}
k = 0;
while(low <= high) arr[low++] = temp[k++];
return;
}
void MSort(int arr[], int low, int high, int temp[]){
if(low < high){
int mid = low + (high-low)/2;
MSort(arr, low, mid, temp);
MSort(arr, mid+1, high, temp);
Merge(arr, low, mid, high, temp);
}
return ;
}
void MergeSort(int arr[], int length){
int* temp = new int[length];
MSort(arr, 0, length-1, temp);
return;a
}
int main() {
int arr[] = {234,3,54,673,46,35,1246,2,624,789};
MergeSort(arr, 10);
for(int i=0; i<10; ++i)
printf("%d ", arr[i]);
system("pause");
}
12-22