#ifndef MERGE_SORT_H #define MERGE_SORT_H #include<string> static int count =0; template<class T,int n> void merge(T* s,int i,int j,int m)//s : i.....,m,m+1,.....j { T* ls = new T[m-i+2]; memcpy(ls,s+i,(m-i+1)*sizeof(T)); ls[m-i+1]=INT_MAX; T* rs = new T[j-m+1]; memcpy(rs,s+m+1,(j-m)*sizeof(T)); rs[j-m]=INT_MAX; int p=0,q=0; for(int k=0;k<j-i+1;k++) { if(ls[p]>rs[q]) { s[i+k]=rs[q++]; count++ } else s[i+k]=ls[p++]; } } template<class T,int n> void merge_sort(T* s,int i=0,int j=n-1) { if(i==j) return; int mid=(i+j)/2; merge_sort<T,n>(s,i,mid); merge_sort<T,n>(s,mid+1,j); merge<T,n>(s,i,j,mid); } #endif