1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include<iostream> using namespace std; void merge(int *L1,int *L2,const int left, const int mid, const int right){ for(int k=left; k<=right; k++){ L2[k]= L1[k]; } int s1 = left, s2 = mid +1 ; int t = left; while(s1<=mid && s2<=right){ if(L2[s2] < L2[s1]) L1[t++] = L2[s2++]; else L1[t++] = L2[s1++]; } while(s1<=mid) L1[t++] = L2[s1++]; while(s2<=right) L1[t++] = L2[s2++]; } void MergeSort(int *L, int *L2, int left, int right){ if(left >= right) return; int mid = (left+right)/2; MergeSort(L,L2,left,mid); MergeSort(L,L2,mid+1,right); merge(L,L2,left,mid,right); } int main(){ int n; cin >> n; int *L = new int[n]; int *L2 = new int[n]; for(int k=0;k<n;k++){ cin >> L[k]; L2[k] = 0; } MergeSort(L,L2,0, n-1); for(int i=0; i<n; i++){ cout << L[i]<< " "; } }
|