Created
April 29, 2015 15:23
-
-
Save cagedmantis/2828fa5c8f5804e52d14 to your computer and use it in GitHub Desktop.
merge sort c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <queue> | |
template<class T> | |
void merge(std::vector<T> &vec, int low, int mid, int high) | |
{ | |
int index = low; | |
std::queue<T> left; | |
std::queue<T> right; | |
for (int i = low; i <= mid; i++) { | |
left.push(vec[i]); | |
} | |
for (int i = mid + 1; i <= high; i++) { | |
right.push(vec[i]); | |
} | |
while (!left.emtpy() && !right.empty()) { | |
if (left.front() < right.front()) { | |
vec[index] = left.front(); | |
left.pop(); | |
} | |
else { | |
vec[index] = right.front(); | |
right.pop(); | |
} | |
index++; | |
} | |
while (!left.empty()) { | |
vec[index] = left.front(); | |
left.pop(); | |
index++; | |
} | |
while (!right.empty()) { | |
vec[index] = right.front(); | |
right.pop(); | |
index++; | |
} | |
} | |
template<class T> | |
void merge_sort(std::vector<T> &vec, int low, int high) | |
{ | |
if (low < high) { | |
int middle = (low + high) / 2; | |
merge_sort(vec, low, middle); | |
merge_sort(vec, middle + 1, high); | |
merge(vec, low, middle, high); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment