Skip to content

Instantly share code, notes, and snippets.

@rachtsingh
Last active January 3, 2016 00:09
Show Gist options
  • Save rachtsingh/8381117 to your computer and use it in GitHub Desktop.
Save rachtsingh/8381117 to your computer and use it in GitHub Desktop.
issues
int acc[NMAX];
void merge(int low, int mid, int high){
// one block from [low --> mid] and one from [mid + 1 --> high]
// sizes (mid - low + 1), and (high - mid)
// this one actually changes their places on the original array
vector<long long> lower(acc + low, acc + mid + 1);
vector<long long> higher(acc + mid + 1, acc + high + 1);
int i = 0; // for lower
int j = 0; // for higher
for (int index = low; index <= high; index++){
if (lower[i] <= higher[j]){
acc[index] = lower[i];
i++;
}
else {
acc[index] = higher[j];
j++;
}
}
}
void mergesort(int low, int high){
// pass indexes to the array acc in, and merge sort those sections
// thses are inclusive array indices
if(low >= high){
return;
}
int mid = (low + high)/2;
mergesort(low, mid);
mergesort(mid + 1, high);
merge(low, mid, high);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment