Skip to content

Commit 51e90dd

Browse files
Added Parallel MergeSort
1 parent c74cc57 commit 51e90dd

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

Algorithms.NET.Debug/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
List<double> unsortedRand = new() { 8, 9, 3, 3, 7, 1 };
77
List<double> unsortedLessThan1 = new() { .8, .9, .3, .3, .1,0 };
88

9-
var sortedDesc = Algorithms.NET.Sorting.MergeSort.MergeSortAlgorithm.SortDescending(unsortedAsc);
9+
var sortedDesc = Algorithms.NET.Sorting.MergeSort.MergeSortAlgorithm.SortDescendingParallel(unsortedAsc);
1010
var sortedAsc = Algorithms.NET.Sorting.BucketSort.BucketSortAlgorithm.SortAscending(unsortedLessThan1);
1111

1212

@@ -36,7 +36,7 @@
3636

3737

3838

39-
//var sortingBenchmarkSummary = BenchmarkRunner.Run<SortingBenchmark>();
39+
var sortingBenchmarkSummary = BenchmarkRunner.Run<SortingBenchmark>();
4040
//var searchingBenchmarkSummary = BenchmarkRunner.Run<SearchingBenchmark>();
4141

4242

Algorithms.NET.Debug/SortingBenchmark.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class SortingBenchmark
1717
public SortingBenchmark()
1818
{
1919
var rand = new Random();
20-
for (int i = 0; i < 10000; i++)
20+
for (int i = 0; i < 50000; i++)
2121
{
2222
double number = rand.NextDouble();
2323
_unsorted.Add(number);
@@ -46,6 +46,10 @@ public void InsertionSort() =>
4646
public void MergeSort() =>
4747
MergeSortAlgorithm.SortAscending(_unsorted);
4848

49+
[Benchmark]
50+
public void MergeSortParallel() =>
51+
MergeSortAlgorithm.SortAscendingParallel(_unsorted);
52+
4953
[Benchmark]
5054
public void QuickSort() =>
5155
QuickSortAlgorithm.SortAscending(_unsorted);

Algorithms.NET/Sorting/MergeSort/MergeSortAlgorithm.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Threading.Tasks;
23

34
namespace Algorithms.NET.Sorting.MergeSort
45
{
@@ -26,6 +27,29 @@ public static List<double> SortDescending(List<double> list)
2627
return Sort(sortedList, true);
2728
}
2829

30+
31+
/// <summary>
32+
/// Sorting a list of numbers in ascending order using MergeSort algorithm, Time complexity of O(n Log n).
33+
/// </summary>
34+
/// <param name="list">List of numbers to sort</param>
35+
/// <returns>Sorted list in ascending order.</returns>
36+
public static List<double> SortAscendingParallel(List<double> list)
37+
{
38+
List<double> sortedList = new List<double>(list);
39+
return SortParallel(sortedList, false);
40+
}
41+
42+
/// <summary>
43+
/// Sorting a list of numbers in descending order using MergeSort algorithm, Time complexity of O(n Log n).
44+
/// </summary>
45+
/// <param name="list">List of numbers to sort</param>
46+
/// <returns>Sorted list in descending order.</returns>
47+
public static List<double> SortDescendingParallel(List<double> list)
48+
{
49+
List<double> sortedList = new List<double>(list);
50+
return SortParallel(sortedList, true);
51+
}
52+
2953
/// <summary>
3054
/// Sorting a list of numbers using MergeSort algorithm, Time complexity of O(n Log n).
3155
/// </summary>
@@ -61,6 +85,50 @@ public static List<double> Sort(List<double> list, bool sortDescending)
6185
return list;
6286
}
6387

88+
89+
/// <summary>
90+
/// Sorting a list of numbers using MergeSort algorithm, Time complexity of O(n Log n).
91+
/// </summary>
92+
/// <param name="list">List of numbers to sort</param>
93+
/// <param name="sortDescending">Boolean value specifying whether sorting should be done in descending order</param>
94+
/// <returns>A sorted list</returns>
95+
public static List<double> SortParallel(List<double> list, bool sortDescending)
96+
{
97+
//We stop recursion when size is 1, which means an array of one element is sorted.
98+
if (list.Count < 2)
99+
return list;
100+
101+
//Divide list in half.
102+
int middle = list.Count / 2;
103+
104+
List<double> left = new List<double>();
105+
List<double> right = new List<double>();
106+
107+
//Fill each half
108+
for (int i = 0; i < middle; i++)
109+
left.Add(list[i]);
110+
111+
for (int j = middle; j < list.Count; j++)
112+
right.Add(list[j]);
113+
114+
//Sort each half, use parallel execution when input size is big to help improve performance.
115+
116+
if (left.Count > 10000)
117+
Parallel.Invoke(() => SortParallel(left, sortDescending), () => SortParallel(right, sortDescending));
118+
else
119+
{
120+
SortParallel(left, sortDescending);
121+
SortParallel(right, sortDescending);
122+
}
123+
124+
125+
//Merge the results
126+
Merge(left, right, list, sortDescending);
127+
128+
return list;
129+
}
130+
131+
64132
/// <summary>
65133
/// Merging two arrays in correct order according to the boolean value of sortDescending
66134
/// </summary>

0 commit comments

Comments
 (0)