UNKNOWN //************************************** // Name: Merge Sort // Description:A simple implementation of Merge sort. // By: Puneet Jindal // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //This code is copyrighted and has limited warranties. //Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.13840/lngWId.3/qx/vb/scripts/ShowCode.htm //for details. //************************************** #include<iostream> #define LIM 32 using namespace std; void Merge(int *arr, int p, int mid, int q) // One array E [p,mid] { int *temp_arr = new int[q-p+1]; int i = p; int k = mid + 1 ; int temp_arr_iter = 0; while(true) { if(i > mid && k > q) break; else if(i <= mid && k > q) { while(i <= mid) { temp_arr[temp_arr_iter] = arr[i]; ++i; ++temp_arr_iter; } break; } else if(i > mid && k <= q) { while(k <= q) { temp_arr[temp_arr_iter] = arr[k]; ++k; ++temp_arr_iter; } break; } else if(i <= mid && arr[i] <= arr[k]) { temp_arr[temp_arr_iter] = arr[i]; ++i; ++temp_arr_iter; } else if(k <= q && arr[k] <= arr[i]) { temp_arr[temp_arr_iter] = arr[k]; ++k; ++temp_arr_iter; } } for(int x = 0 ; x < q-p+1 ; ++x) { arr[x + p] = temp_arr[x]; } delete []temp_arr; } void mSort(int *arr, int p, int q) { if(p < q) { int mid = (p+q)/2; mSort(arr,p,mid); mSort(arr,mid+1,q); Merge(arr,p,mid,q); } } int main() { int arr[LIM]; for(int i = 0 ; i < LIM ; ++i) cin>>arr[i]; mSort(arr,0,LIM-1); for(int i = 0 ; i < LIM ; ++i) cout<<arr[i]<<" "; cout<<endl; return 0; }