Commit 5041e021 authored by ShahRukh's avatar ShahRukh

change implementation to merge-sort

parent 0ea14c1d
No preview for this file type
#include<vector> #include<iostream>
#include"sorting.h"
using namespace std; using namespace std;
vector<int> sort_custom(vector<int> v){
int i, j; vector<int> mergeSorting(vector<int> arr);
int n=v.size(); vector<int> mergeSorting(vector<int> v);
for (i = 0; i < n-1; i++){ vector<int> merge(vector<int> first,vector<int> second){
for (j = 0; j < n-i-1; j++){ vector<int> v;
if (v[j] > v[j+1]){ int countFirst = 0;
int temp; int countSecond = 0;
temp=v[j]; int size = first.size()+second.size();
v[j]=v[j+1]; int left = 1;
v[j+1]=temp; for (int i=0; i<size;i++){
if(countFirst<first.size() && countSecond<second.size() ){
left = (first[countFirst] < second[countSecond]);
} }
else if (countFirst<first.size()) {
left = 1;
} }
else{
left = 0;
}
if (left == 1){
v.push_back(first[countFirst]);
if ( first.size()> countFirst){
countFirst+=1;
} }
}
else{
v.push_back(second[countSecond]);
if (second.size()> countSecond){
countSecond+=1;
}
}
}
return v;
}
vector<int> mergeSorting(vector<int> v){
if (v.size() <= 1){
return v; return v;
}
vector<int> first(v.cbegin(), v.cbegin()+(v.size()/2));
vector<int> second(v.cbegin()+(v.size()/2), v.cend());
return merge(mergeSorting(first),mergeSorting(second));
}
vector<int> sort_custom(vector<int> v){
vector<int> sortVec = mergeSorting(v);
return sortVec;
} }
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