|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace Algorithms.NET.Sorting.BucketSort |
| 6 | +{ |
| 7 | + public class BucketSortAlgorithm |
| 8 | + { |
| 9 | + /// <summary> |
| 10 | + /// Sorting a list of numbers in ascending order using BucketSort algorithm, Time complexity of O(n). |
| 11 | + /// </summary> |
| 12 | + /// <param name="list">List of numbers to sort</param> |
| 13 | + /// <returns>Sorted List in ascending order.</returns> |
| 14 | + public static List<double> SortAscending(List<double> list) |
| 15 | + { |
| 16 | + return Sort(list, false); |
| 17 | + } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Sorting a list of numbers in ascending order using BucketSort algorithm,, Time complexity of O(n). |
| 21 | + /// </summary> |
| 22 | + /// <param name="list">List of numbers to sort</param> |
| 23 | + /// <returns>Sorted List in Descending order.</returns> |
| 24 | + public static List<double> SortDescending(List<double> list) |
| 25 | + { |
| 26 | + return Sort(list, true); |
| 27 | + } |
| 28 | + |
| 29 | + private static List<double> Sort(List<double> list, bool sortDescending) |
| 30 | + { |
| 31 | + int NUMBER_OF_BUCKETS = 10000; |
| 32 | + //Copy the original list to avoid mutation |
| 33 | + List<double> sortedList = new List<double>(list); |
| 34 | + //Create buckets array of size NUMBER_OF_BUCKETS |
| 35 | + List<double>[] buckets = new List<double>[NUMBER_OF_BUCKETS]; |
| 36 | + |
| 37 | + //Initilize inner lists |
| 38 | + for (int i = 0; i < buckets.Length; i++) |
| 39 | + { |
| 40 | + buckets[i] = new List<double>(); |
| 41 | + } |
| 42 | + |
| 43 | + double max = 0; |
| 44 | + //Find the max |
| 45 | + for(int i = 0;i < sortedList.Count;i++) |
| 46 | + { |
| 47 | + if (sortedList[i] > max) |
| 48 | + max = sortedList[i]; |
| 49 | + } |
| 50 | + |
| 51 | + int divider = 1; |
| 52 | + //Find the divider which we are going to divide numbers that are greater than 1 |
| 53 | + while(max >= 1) |
| 54 | + { |
| 55 | + max /= 10; |
| 56 | + divider *= 10; |
| 57 | + } |
| 58 | + //We divide the numbers |
| 59 | + if(divider > 1) |
| 60 | + for (int i = 0; i < sortedList.Count; i++) |
| 61 | + { |
| 62 | + sortedList[i] = sortedList[i] / divider; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + // We insert the numbers to the corresponding bucket |
| 68 | + for (int i = 0; i < sortedList.Count; i++) |
| 69 | + { |
| 70 | + int index = (int)(sortedList[i] * NUMBER_OF_BUCKETS); |
| 71 | + buckets[index].Add(sortedList[i]); |
| 72 | + } |
| 73 | + |
| 74 | + // We sort the inner lists using MergeSort, you can use QuickSort too |
| 75 | + for (int i = 0; i < NUMBER_OF_BUCKETS; i++) |
| 76 | + for (int j = 0; j < buckets[i].Count; j++) |
| 77 | + { |
| 78 | + buckets[i] = MergeSortFunc(buckets[i]); |
| 79 | + } |
| 80 | + |
| 81 | + // We insert elements based on if we want them sorted ascending or descending |
| 82 | + if (!sortDescending) |
| 83 | + { |
| 84 | + int k = 0; |
| 85 | + for (int i = 0; i < buckets.Length; i++) |
| 86 | + for (int j = 0; j < buckets[i].Count; j++) |
| 87 | + sortedList[k++] = buckets[i][j] * divider; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + int k = sortedList.Count - 1; |
| 92 | + for (int i = 0; i < buckets.Length; i++) |
| 93 | + for (int j = 0; j < buckets[i].Count; j++) |
| 94 | + sortedList[k--] = buckets[i][j] * divider; |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + return sortedList; |
| 101 | + } |
| 102 | + |
| 103 | + private static List<double> MergeSortFunc(List<double> list) |
| 104 | + { |
| 105 | + if (list.Count < 2) |
| 106 | + return list; |
| 107 | + |
| 108 | + int middle = list.Count / 2; |
| 109 | + |
| 110 | + List<double> left = new List<double>(); |
| 111 | + List<double> right = new List<double>(); |
| 112 | + |
| 113 | + for (int i = 0; i < middle; i++) |
| 114 | + left.Add(list[i]); |
| 115 | + |
| 116 | + for (int i = middle; i < list.Count; i++) |
| 117 | + right.Add(list[i]); |
| 118 | + |
| 119 | + MergeSortFunc(left); |
| 120 | + MergeSortFunc(right); |
| 121 | + |
| 122 | + Merge(left, right, list); |
| 123 | + |
| 124 | + return list; |
| 125 | + } |
| 126 | + |
| 127 | + private static void Merge(List<double> left, List<double> right, List<double> list) |
| 128 | + { |
| 129 | + int i = 0, j = 0, k = 0; |
| 130 | + |
| 131 | + while (i < left.Count && j < right.Count) |
| 132 | + { |
| 133 | + if (left[i] < right[j]) |
| 134 | + list[k++] = left[i++]; |
| 135 | + else list[k++] = right[j++]; |
| 136 | + } |
| 137 | + |
| 138 | + while (i < left.Count) |
| 139 | + { |
| 140 | + list[k++] = left[i++]; |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + while (j < right.Count) |
| 145 | + { |
| 146 | + list[k++] = right[j++]; |
| 147 | + |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments