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> mergeSorting(vector<int> arr);
vector<int> mergeSorting(vector<int> v);
vector<int> merge(vector<int> first,vector<int> second){
vector<int> v;
int countFirst = 0;
int countSecond = 0;
int size = first.size()+second.size();
int left = 1;
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;
}
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> sort_custom(vector<int> v){
int i, j; vector<int> sortVec = mergeSorting(v);
int n=v.size(); return sortVec;
for (i = 0; i < n-1; i++){ }
for (j = 0; j < n-i-1; j++){
if (v[j] > v[j+1]){
int temp;
temp=v[j];
v[j]=v[j+1];
v[j+1]=temp;
}
}
}
return v;
}
\ 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