标签:util asi sid tac int ++ could void nts
/*crate a new array to put the elements sorted*/
void MergeSort(int a[], int n){
int *TmpArray;
TmpArray = (int*)malloc(n*sizeof(int));
if (TmpArray != NULL){
MSort(a, TmpArray, 0, n - 1);
free(TmpArray);
}
}
//We could utilize the stack to understand the Sort and Merge
void MSort(int a[], int TmpArray[], int left, int right){
int center;
if (left < right){
center = (left + right) / 2;
MSort(a, TmpArray, left, center);// sort the left side
MSort(a, TmpArray, center + 1, right);// sort the right side
Merge(a, TmpArray,left,center + 1, right);
}
}
/*two sorted array can be merged easily*/
void Merge(int a[], int TmpArray[], int left, int mid, int right){
int i,Tmp,NumElements;
Tmp = left;
NumElements = right - left + 1;
while (left <= mid-1, mid <= right){
if (a[left] <= a[mid])
TmpArray[Tmp++] = a[left++];
else
TmpArray[Tmp++] = a[mid++];
}
while (left <= mid - 1)
TmpArray[Tmp++] = a[left++];
while (mid <= right)
TmpArray[Tmp++] = a[mid++];
for (i = 0; i < NumElements; i++, right--)
a[right] = TmpArray[right];
}
标签:util asi sid tac int ++ could void nts
原文地址:http://www.cnblogs.com/Vincent-Zy/p/6193413.html