Created
December 5, 2016 21:40
-
-
Save fellipecaetano/3d2800be657cecb4a05da7f93c823696 to your computer and use it in GitHub Desktop.
Merge two subarrays of Time according to greedy criterion
This file contains hidden or 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
void merge_subarrays(vector<Time> &array, int low, int mid, int high) { | |
vector<Time> left; | |
left.resize(mid - low); | |
vector<Time> right; | |
right.resize(high - mid); | |
for (int i = 0; i < left.size(); i++) { | |
left[i] = array[low + i]; | |
} | |
for (int i = 0; i < right.size(); i++) { | |
right[i] = array[mid + i]; | |
} | |
int i = 0, j = 0; | |
for (int k = low; k < high; k++) { | |
if (i == left.size()) { | |
array[k] = right[j++]; | |
} else if (j == right.size()) { | |
array[k] = left[i++]; | |
} else if (greedy_criterion(left[i]) >= greedy_criterion(right[j])) { | |
array[k] = left[i++]; | |
} else { | |
array[k] = right[j++]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment