Commit c547aff3 authored by NIRMAL RAJPUT's avatar NIRMAL RAJPUT

update merge-sort to reflect API update

parent a3dd9cd6
#include "sorting.h" #include "sorting.h"
using namespace std;
vector<int> merge_fun(vector<int> array1, vector<int> array2){ void merge(vector<int>& inp, int start, int middle, int end) {
int n1=array1.size(), n2=array2.size(),count1=0,count2=0; int p = start,q = middle+1;
vector<int> merged(n1+n2); int array[end-start+1] ;
for(int i=0;i<n1+n2;i++){ int k=0 ;
if(count1>=n1){ for(int i=start;i<=end;i++) {
merged[i]=array2[count2]; if(p>middle)
count2++; array[k++] = inp[q++] ;
} else if(q>end)
else if(count2>=n2){ array[k++] = inp[p++];
merged[i]=array1[count1]; else if(inp[p]<inp[q])
count1++; array[k++] = inp[p++];
} else
else{ array[k++] = inp[q++];
if(array1[count1]<=array2[count2]){ }
merged[i]=array1[count1]; for(int i=start ; i<=end ; i++){
count1++; inp[i]=array[i-start] ;
} }
return merged;
} }
vector<int> sort_custom(vector<int> inp){
int n=inp.size();
if(n==1){ void merge_sort(vector<int>& inp, int start, int end)
return inp; {
} if(start < end){
else int middle = (start + end)/2 ;
{ merge_sort(inp,start,middle) ;
int n1=n/2; merge_sort(inp,middle+1,end) ;
int n2=n-n1; merge(inp,start,middle,end);
vector<int> array1(n1),array2(n2);
for(int i=0;i<n;i++){
else if(i<n1+n2)
array2[i - n1]=inp[i];
vector<int> out1=sort_custom(array1);
vector<int> out2=sort_custom(array2);
vector<int> ans=merge_fun(out1,out2);
return ans;
} }
vector<int> sort_custom(vector<int> inp, int startidx, int endidx){
merge_sort(inp,startidx,endidx) ;
return inp;
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment