Skip to content

Commit 0b7e09f

Browse files
Added bucketSort algorithm
1 parent 50e947e commit 0b7e09f

File tree

4 files changed

+170
-9
lines changed

4 files changed

+170
-9
lines changed

Algorithms.NET.Debug/BenchmarkDemo.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Algorithms.NET.Sorting.BubbleSort;
2+
using Algorithms.NET.Sorting.BucketSort;
23
using Algorithms.NET.Sorting.InsertionSort;
34
using Algorithms.NET.Sorting.MergeSort;
45
using Algorithms.NET.Sorting.QuickSort;
@@ -18,12 +19,12 @@ public BenchmarkDemo()
1819
var rand = new Random();
1920
for (int i = 0; i < 10000; i++)
2021
{
21-
_unsorted.Add(rand.Next(1000000));
22+
_unsorted.Add(rand.NextDouble());
2223
_unsorted2.Add(rand.Next(1000000));
2324
}
2425
}
2526

26-
[Benchmark]
27+
//[Benchmark]
2728
public void BuiltInSort() =>
2829
_unsorted2.Sort();
2930

@@ -44,9 +45,13 @@ public void InsertionSort() =>
4445
public void MergeSort() =>
4546
MergeSortAlgorithm.SortAscending(_unsorted);
4647

47-
[Benchmark]
48+
//[Benchmark]
4849
public void QuickSort() =>
4950
QuickSortAlgorithm.SortAscending(_unsorted);
5051

52+
[Benchmark]
53+
public void BucketSort() =>
54+
BucketSortAlgorithm.SortAscending(_unsorted);
55+
5156
}
5257
}

Algorithms.NET.Debug/Program.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
List<double> unsortedAsc = new() { 0, 1, 2, 3, 4, 5 };
1+
using Algorithms.NET.Debug;
2+
using BenchmarkDotNet.Running;
3+
4+
List<double> unsortedAsc = new() { 0, 1, 2, 3, 4, 5 };
25
List<double> unsortedDesc = new() { 5, 4, 3, 2, 1, 0 };
3-
List<int> unsortedRand = new() { 8, 9, 3, 3, 10, 1 };
6+
List<double> unsortedRand = new() { 8, 9, 3, 3, 100000, 1 };
7+
List<double> unsorted01 = new() { .8, .9, .3, .3, .1,0 };
48

59
//var sortedDesc = Algorithms.NET.Sorting.MergeSort.MergeSortAlgorithm.SortDescending(unsortedAsc);
6-
var sortedAsc = Algorithms.NET.Sorting.CountingSort.CountingSortAlgorithm.SortAscending(unsortedRand.ToArray());
7-
var sortedDesc = Algorithms.NET.Sorting.CountingSort.CountingSortAlgorithm.SortDescending(unsortedRand.ToArray());
10+
var sortedAsc = Algorithms.NET.Sorting.BucketSort.BucketSortAlgorithm.SortAscending(unsorted01);
11+
var sortedDesc = Algorithms.NET.Sorting.BucketSort.BucketSortAlgorithm.SortDescending(unsorted01);
12+
//var sortedDesc = Algorithms.NET.Sorting.CountingSort.CountingSortAlgorithm.SortDescending(unsortedRand.ToArray());
813

914

1015
foreach (var item in sortedDesc)
@@ -24,7 +29,7 @@
2429
// _unsortedAsc.Add(rand.Next(1000000));
2530
//}
2631

27-
//var summary = BenchmarkRunner.Run<BenchmarkDemo>();
32+
var summary = BenchmarkRunner.Run<BenchmarkDemo>();
2833

2934

3035

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
}

Algorithms.NET/Sorting/MergeSort/MergeSortAlgorithm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static List<double> SortDescending(List<double> list)
3232
/// <param name="list">List of numbers to sort</param>
3333
/// <param name="sortDescending">Boolean value specifying whether sorting should be done in descending order</param>
3434
/// <returns>A sorted list</returns>
35-
private static List<double> Sort(List<double> list, bool sortDescending)
35+
public static List<double> Sort(List<double> list, bool sortDescending)
3636
{
3737
//We stop recursion when size is 1, which means an array of one element is sorted.
3838
if (list.Count < 2)

0 commit comments

Comments
 (0)