|
1 | 1 | using System.Collections.Generic;
|
| 2 | +using System.Threading.Tasks; |
2 | 3 |
|
3 | 4 | namespace Algorithms.NET.Sorting.MergeSort
|
4 | 5 | {
|
@@ -26,6 +27,29 @@ public static List<double> SortDescending(List<double> list)
|
26 | 27 | return Sort(sortedList, true);
|
27 | 28 | }
|
28 | 29 |
|
| 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 | + |
29 | 53 | /// <summary>
|
30 | 54 | /// Sorting a list of numbers using MergeSort algorithm, Time complexity of O(n Log n).
|
31 | 55 | /// </summary>
|
@@ -61,6 +85,50 @@ public static List<double> Sort(List<double> list, bool sortDescending)
|
61 | 85 | return list;
|
62 | 86 | }
|
63 | 87 |
|
| 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 | + |
64 | 132 | /// <summary>
|
65 | 133 | /// Merging two arrays in correct order according to the boolean value of sortDescending
|
66 | 134 | /// </summary>
|
|
0 commit comments