Skip to content

Instantly share code, notes, and snippets.

@TomasDrozdik
Last active May 20, 2019 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomasDrozdik/39feab5c461145a2b34f554dbfad7de6 to your computer and use it in GitHub Desktop.
Save TomasDrozdik/39feab5c461145a2b34f554dbfad7de6 to your computer and use it in GitHub Desktop.
private void ParallelSort<T>(List<T> list, Comparer<T> comparer) {
void MergeSort(List<T> l, Comparer<T> comp, int low, int high) {
int mid = (high - low) / 2;
var t1 = new Thread(() => MergeSort(l, comp, low, mid));
t1.Start();
t1.Join();
MergeSort(l, comp, mid, high);
Merge(l, comp, low, mid, high);
}
void Merge(List<T> l, Comparer<T> comp, int low, int mid, int high) {
var copy = l.GetRange(low, high - low);
int i = low;
while (i < mid && i + mid < high) {
l[i++] = comp.Compare(copy[low], copy[mid]) < 0 ? copy[low++] : copy[mid++];
}
while (i < mid) {
l[i++] = copy[low++];
}
while (mid < high) {
l[i++] = copy[mid++];
}
}
MergeSort(list, comparer, 0, list.Count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment