Last active
May 20, 2019 17:37
-
-
Save TomasDrozdik/39feab5c461145a2b34f554dbfad7de6 to your computer and use it in GitHub Desktop.
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
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