#define_CRT_SECURE_NO_WARNINGS1#include<iostream>#include<string.h>usingnamespace std;//recursionvoid_merge_sort(int* a,int*at,int start,int end){if(start >= end)return;int m = start +(end - start)/2;// m = (start + end) / 2,layer_count is more 1.int start1 = start, end1 = m;int start2 = m +1, end2 = end;//split array,the same layer split returns the same layer_merge_sort(a,at, start1, end1);_merge_sort(a,at, start2, end2);int i = start;//start is the same split returns array start,i recode array_at placewhile(start1 <= end1 && start2 <= end2)
at[i++]= a[start1]< a[start2]? a[start1++]: a[start2++];//back_++ so nice!while(start1 <= end1)
at[i++]= a[start1++];//split_left_array and after start1 , place array_at.while(start2 <= end2)
at[i++]= a[start2++];//split_right_array and after start1 , place array_at.//link the same arrays.//memcpy(a+start, at+start,(end-start+1)*sizeof(int));while(start <= end){
a[start]= at[start];
start++;}}voidmerge_sort1(int*a,int n){if(n <2)return;int*at=newint[n];if(at){_merge_sort(a, at,0, n -1);delete[]at;}else
cout <<"allot fail ";}////circulationvoidmerge_sort2(int* a,unsignedint n){if(n <2)return;int* p = a;//point array to be sorted int* at =newint[n];if(!at){
cout <<"allot fail ";return;}int c;//1,2,4,8,16,...int i;for(c =1; c < n; c *=2){//conttrol circulate_countfor(i =0; i < n; i += c *2){//conttrol split_part_count,i is train_startint l = i;int r =(i + c *2)< n ?(i + c *2): n;//r is part2__end_next not part2_endint m =(i + c)< n ?(i + c): n;//m is part1_end_next not part1_endint ml = m;int ii = i;while(l < m && ml < r)
at[ii++]= p[l]< p[ml]? p[l++]: p[ml++];while(l < m)
at[ii++]= p[l++];while(ml < r)
at[ii++]= p[ml++];}//array to be sorted changeint* pt = p;
p = at;
at = pt;}if(p != a){for(int j =0; j < n; j++)
a[j]= p[j];
at = p;}delete[]at;}intmain(){int a[]={44,3,38,5,47,15,36,26,27,2,46,4,19,50,48};merge_sort1(a,sizeof(a)/sizeof(a[0]));for(constauto& e : a)
cout << e <<" ";
cout << endl;int b[]={44,3,38,5,47,15,36,26,27,2,46,4,19,50,48};merge_sort2(b,sizeof(b)/sizeof(b[0]));for(constauto& e : b)
cout << e <<" ";
cout << endl;return0;}