void merge1(int *a,int left,int mid,int right){ int n1=mid-left+1; int n2=right-mid; int *arr1=new int[n1]; int *arr2=new int[n2]; for(int i=0;i<n1;i++) arr1[i]=a[left+i]; for(int i=0;i<n2;i++) arr2[i]=a[mid+i+1]; int i=0,j=0,k=left; while(i<n1&&j<n2){ if(arr1[i]<arr2[j]) a[k++]=arr1[i++]; else a[k++]=arr2[j++]; } while(i<n1){ a[k++]=arr1[i++]; } while(j<n2){ a[k++]=arr2[j++]; } } void merge2(int *a,int left,int mid,int right){ int i=left; int j=mid+1; int *temp=new int[right-left+1]; int t=0; while(i<=mid&&j<=right) if(a[i]<=a[j]) temp[t++]=a[i++]; else temp[t++]=a[j++]; while(i<=mid) temp[t++]=a[i++]; while(j<=right) temp[t++]=a[j++]; t=0; while(left<=right) a[left++]=temp[t++]; } void mergeSort(int *a,int left,int right){ if(left<right){ int mid=(left+right)/2; mergeSort(a,left,mid); mergeSort(a,mid+1,right); merge1(a,left,mid,right); //merge2(a,left,mid,right); } }