Commit 9c324ffc authored by NIRMAL RAJPUT's avatar NIRMAL RAJPUT

change implementation to merge-sort

parent 4bd8f745
#include "sorting.h"
#include<iostream> #include<iostream>
vector<int> sort_custom(vector<int> inp){ #include<vector>
for (int i=0;i<inp.size();i++){ #include "sorting.h"
for (int j=1;j<inp.size();j++){
if (inp[j-1]>inp[j]){ using namespace std;
int temp=inp[j];
inp[j]=inp[j-1]; vector<int> merge_fun(vector<int> array1, vector<int> array2){
inp[j-1]=temp; int n1=array1.size(), n2=array2.size(),count1=0,count2=0;
vector<int> merged(n1+n2);
for(int i=0;i<n1+n2;i++){
if(count1>=n1){
merged[i]=array2[count2];
count2++;
}
else if(count2>=n2){
merged[i]=array1[count1];
count1++;
}
else{
if(array1[count1]<=array2[count2]){
merged[i]=array1[count1];
count1++;
}
else{
merged[i]=array2[count2];
count2++;
} }
} }
} }
return merged;
}
vector<int> sort_custom(vector<int> inp){
int n=inp.size();
if(n==1){
return inp; return inp;
}
else
{
int n1=n/2;
int n2=n-n1;
vector<int> array1(n1),array2(n2);
for(int i=0;i<n;i++){
if(i<n1)
array1[i]=inp[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;
}
} }
\ 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