Commit 0d9031ea authored by POTHULA SAI VISHAL's avatar POTHULA SAI VISHAL

change implementation to merge-sort

parent 6b3ddc6b
...@@ -3,21 +3,45 @@ ...@@ -3,21 +3,45 @@
#include "sorting.h" #include "sorting.h"
using namespace std; using namespace std;
vector<int> sort_custom(vector<int> c){ vector<int> sort_custom(vector<int> m)
bool pass = false; //have we passed through the cay without a swap? {
int temp; if (m.size() <= 1)
return m;
vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;
for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}
while (!pass){ for (int i = middle; i < (int)m.size(); i++) {
pass = true; right.push_back(m[i]);
for (int i=0; i<c.size()-1; i++){ }
if (c[i] > c[i+1]){
pass = false; left = sort_custom(left);
temp = c[i]; right = sort_custom(right);
c[i] = c[i+1]; while ((int)left.size() > 0 || (int)right.size() > 0) {
c[i+1] = temp; if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
} }
} }
}
return c; return result;
} }
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