|
1 | | -using BenchmarkDotNet.Attributes; |
| 1 | +namespace BucketSort; |
2 | 2 |
|
3 | | -namespace BucketSort |
| 3 | +public class BucketSortMethods |
4 | 4 | { |
5 | | - public class BucketSortMethods |
| 5 | + public int[] SortArray(int[] array) |
6 | 6 | { |
7 | | - public IEnumerable<object[]> SampleArrays() |
| 7 | + if (array.Length <= 1) |
8 | 8 | { |
9 | | - yield return new object[] { CreateRandomArray(200), "Small Unsorted" }; |
10 | | - yield return new object[] { CreateRandomArray(2000), "Medium Unsorted" }; |
11 | | - yield return new object[] { CreateRandomArray(20000), "Large Unsorted" }; |
12 | | - yield return new object[] { CreateSortedArray(200), "Small Sorted" }; |
13 | | - yield return new object[] { CreateSortedArray(2000), "Medium Sorted" }; |
14 | | - yield return new object[] { CreateSortedArray(20000), "Large Sorted" }; |
| 9 | + return array; |
15 | 10 | } |
16 | 11 |
|
17 | | - [Benchmark] |
18 | | - [ArgumentsSource(nameof(SampleArrays))] |
19 | | - public int[] SortArray(int[] array, string arrayName) |
20 | | - { |
21 | | - List<int> sortedList = new List<int>(); |
22 | | - var minValue = array[0]; |
23 | | - var maxValue = array[0]; |
24 | | - |
25 | | - if (array == null || array.Length <= 1) |
26 | | - { |
27 | | - return array; |
28 | | - } |
| 12 | + List<int> sortedList = []; |
| 13 | + var minValue = array[0]; |
| 14 | + var maxValue = array[0]; |
29 | 15 |
|
30 | | - for (int i = 1; i < array.Length; i++) |
31 | | - { |
32 | | - if (array[i] > maxValue) |
33 | | - maxValue = array[i]; |
34 | | - if (array[i] < minValue) |
35 | | - minValue = array[i]; |
36 | | - } |
| 16 | + for (int i = 1; i < array.Length; i++) |
| 17 | + { |
| 18 | + if (array[i] > maxValue) |
| 19 | + maxValue = array[i]; |
| 20 | + if (array[i] < minValue) |
| 21 | + minValue = array[i]; |
| 22 | + } |
37 | 23 |
|
38 | | - var numberOfBuckets = maxValue - minValue + 1; |
39 | | - List<int>[] bucket = new List<int>[numberOfBuckets]; |
| 24 | + var numberOfBuckets = array.Length; |
| 25 | + var range = (long)maxValue - minValue + 1; |
| 26 | + List<int>[] bucket = new List<int>[numberOfBuckets]; |
40 | 27 |
|
41 | | - for (int i = 0; i < numberOfBuckets; i++) |
42 | | - { |
43 | | - bucket[i] = new List<int>(); |
44 | | - } |
45 | | - |
46 | | - for (int i = 0; i < array.Length; i++) |
47 | | - { |
48 | | - var selectedBucket = (array[i] / numberOfBuckets); |
49 | | - bucket[selectedBucket].Add(array[i]); |
50 | | - } |
| 28 | + for (int i = 0; i < numberOfBuckets; i++) |
| 29 | + { |
| 30 | + bucket[i] = []; |
| 31 | + } |
51 | 32 |
|
52 | | - for (int i = 0; i < numberOfBuckets; i++) |
53 | | - { |
54 | | - int[] temp = BubbleSort(bucket[i]); |
55 | | - sortedList.AddRange(temp); |
56 | | - } |
| 33 | + for (int i = 0; i < array.Length; i++) |
| 34 | + { |
| 35 | + var selectedBucket = (int)((array[i] - (long)minValue) * numberOfBuckets / range); |
| 36 | + bucket[selectedBucket].Add(array[i]); |
| 37 | + } |
57 | 38 |
|
58 | | - return sortedList.ToArray(); |
| 39 | + for (int i = 0; i < numberOfBuckets; i++) |
| 40 | + { |
| 41 | + int[] temp = BubbleSort(bucket[i]); |
| 42 | + sortedList.AddRange(temp); |
59 | 43 | } |
60 | 44 |
|
61 | | - public static int[] BubbleSort(List<int> listInput) |
| 45 | + return sortedList.ToArray(); |
| 46 | + } |
| 47 | + |
| 48 | + public static int[] BubbleSort(List<int> listInput) |
| 49 | + { |
| 50 | + for (int i = 0; i < listInput.Count - 1; i++) |
62 | 51 | { |
63 | | - for (int i = 0; i < listInput.Count; i++) |
| 52 | + for (int j = 0; j < listInput.Count - i - 1; j++) |
64 | 53 | { |
65 | | - for (int j = 0; j < listInput.Count; j++) |
| 54 | + if (listInput[j] > listInput[j + 1]) |
66 | 55 | { |
67 | | - if (listInput[i] < listInput[j]) |
68 | | - { |
69 | | - var temp = listInput[i]; |
70 | | - listInput[i] = listInput[j]; |
71 | | - listInput[j] = temp; |
72 | | - } |
| 56 | + var temp = listInput[j]; |
| 57 | + listInput[j] = listInput[j + 1]; |
| 58 | + listInput[j + 1] = temp; |
73 | 59 | } |
74 | 60 | } |
75 | | - return listInput.ToArray(); |
76 | 61 | } |
77 | 62 |
|
| 63 | + return listInput.ToArray(); |
| 64 | + } |
78 | 65 |
|
79 | | - [Benchmark] |
80 | | - [ArgumentsSource(nameof(SampleArrays))] |
81 | | - public int[] SortArrayOptimized(int[] array, string arrayName) |
| 66 | + public int[] SortArrayOptimized(int[] array) |
| 67 | + { |
| 68 | + if (array.Length <= 1) |
82 | 69 | { |
83 | | - if (array == null || array.Length <= 1) |
84 | | - { |
85 | | - return array; |
86 | | - } |
| 70 | + return array; |
| 71 | + } |
87 | 72 |
|
88 | | - int maxValue = array[0]; |
89 | | - int minValue = array[0]; |
| 73 | + int maxValue = array[0]; |
| 74 | + int minValue = array[0]; |
90 | 75 |
|
91 | | - for (int i = 1; i < array.Length; i++) |
| 76 | + for (int i = 1; i < array.Length; i++) |
| 77 | + { |
| 78 | + if (array[i] > maxValue) |
92 | 79 | { |
93 | | - if (array[i] > maxValue) |
94 | | - { |
95 | | - maxValue = array[i]; |
96 | | - } |
| 80 | + maxValue = array[i]; |
| 81 | + } |
97 | 82 |
|
98 | | - if (array[i] < minValue) |
99 | | - { |
100 | | - minValue = array[i]; |
101 | | - } |
| 83 | + if (array[i] < minValue) |
| 84 | + { |
| 85 | + minValue = array[i]; |
102 | 86 | } |
| 87 | + } |
103 | 88 |
|
104 | | - LinkedList<int>[] bucket = new LinkedList<int>[maxValue - minValue + 1]; |
| 89 | + var range = (long)maxValue - minValue + 1; |
| 90 | + LinkedList<int>[] bucket = new LinkedList<int>[range]; |
105 | 91 |
|
106 | | - for (int i = 0; i < array.Length; i++) |
| 92 | + for (int i = 0; i < array.Length; i++) |
| 93 | + { |
| 94 | + if (bucket[array[i] - minValue] == null) |
107 | 95 | { |
108 | | - if (bucket[array[i] - minValue] == null) |
109 | | - { |
110 | | - bucket[array[i] - minValue] = new LinkedList<int>(); |
111 | | - } |
112 | | - |
113 | | - bucket[array[i] - minValue].AddLast(array[i]); |
| 96 | + bucket[array[i] - minValue] = new LinkedList<int>(); |
114 | 97 | } |
115 | 98 |
|
116 | | - var index = 0; |
117 | | - for (int i = 0; i < bucket.Length; i++) |
| 99 | + bucket[array[i] - minValue].AddLast(array[i]); |
| 100 | + } |
| 101 | + |
| 102 | + var index = 0; |
| 103 | + |
| 104 | + for (int i = 0; i < bucket.Length; i++) |
| 105 | + { |
| 106 | + if (bucket[i] != null) |
118 | 107 | { |
119 | | - if (bucket[i] != null) |
| 108 | + LinkedListNode<int>? node = bucket[i].First; |
| 109 | + |
| 110 | + while (node != null) |
120 | 111 | { |
121 | | - LinkedListNode<int> node = bucket[i].First; |
122 | | - |
123 | | - while (node != null) |
124 | | - { |
125 | | - array[index] = node.Value; |
126 | | - node = node.Next; |
127 | | - index++; |
128 | | - } |
| 112 | + array[index] = node.Value; |
| 113 | + node = node.Next; |
| 114 | + index++; |
129 | 115 | } |
130 | 116 | } |
131 | | - return array; |
132 | 117 | } |
133 | 118 |
|
134 | | - public static int[] CreateRandomArray(int size) |
135 | | - { |
136 | | - var array = new int[size]; |
137 | | - var rand = new Random(); |
| 119 | + return array; |
| 120 | + } |
138 | 121 |
|
139 | | - for (int i = 0; i < size; i++) |
140 | | - array[i] = rand.Next(1, size); |
| 122 | + public static int[] CreateRandomArray(int size) |
| 123 | + { |
| 124 | + var array = new int[size]; |
| 125 | + var rand = new Random(); |
141 | 126 |
|
142 | | - return array; |
143 | | - } |
| 127 | + for (int i = 0; i < size; i++) |
| 128 | + array[i] = rand.Next(1, size); |
144 | 129 |
|
145 | | - public static int[] CreateSortedArray(int size) |
146 | | - { |
147 | | - var array = new int[size]; |
| 130 | + return array; |
| 131 | + } |
148 | 132 |
|
149 | | - for (int i = 0; i < size; i++) |
150 | | - array[i] = i; |
| 133 | + public static int[] CreateSortedArray(int size) |
| 134 | + { |
| 135 | + var array = new int[size]; |
151 | 136 |
|
152 | | - return array; |
153 | | - } |
| 137 | + for (int i = 0; i < size; i++) |
| 138 | + array[i] = i; |
| 139 | + |
| 140 | + return array; |
154 | 141 | } |
155 | 142 | } |
0 commit comments