Skip to content

Commit 6eb545a

Browse files
committed
Bucket Sort
1 parent f5f983d commit 6eb545a

File tree

6 files changed

+126
-3
lines changed

6 files changed

+126
-3
lines changed

DOC/Bucket_Sort.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Bucket Sort
2+
3+
Bucket Sort is a comparison sort algorithm that operates on elements by dividing them into different buckets and then sorting these buckets individually. Each bucket is sorted individually using a separate sorting algorithm or by applying the bucket sort algorithm recursively. Bucket sort is mainly useful when the input es uniformly distributed over a range.
4+
5+
Assume one has the following problem in front of them:
6+
7+
One has been given a large array of floating-point integers lying uniformly between the lower and upper bound. This array now needs to be sorted. A simple way to solve this problem would be to use another sorting algorithm such as Merge sort, Heap sort, or Quicksort. However, these algorithms guarantee a best case time complexity of *O(N log N)* However, using bucket sort, the above task can be completed in *O(N)* time. Let's have a closes look at it.
8+
9+
Consider one needs to create an array of lists, i.e of buckets. Elements now need to be inserted into these buckets on the basis of their properties. Each of these buckets can then be sorted individually using Insertion Sort.
10+
11+
## Implementation
12+
13+
Bucket sort for numbers having interger part
14+
15+
```python
16+
def bucket_sort(data, n_buckets, draw_data, time_tick):
17+
max_element = max(data)
18+
min_element = min(data)
19+
```
20+
21+
Range for buckets
22+
23+
```python
24+
ran = (max_element - min_element) / n_buckets
25+
temp = []
26+
```
27+
28+
Create empty buckets.
29+
30+
```python
31+
for i in range(n_buckets):
32+
temp.append([])
33+
```
34+
35+
Scatter the array elements into the correct bucket.
36+
37+
```python
38+
for i in range(len(data)):
39+
diff = (data[i] - min_element) / ran - int((data[i] - min_element) / ran)
40+
```
41+
42+
Append the boundary elements to the lower array.
43+
44+
```python
45+
if diff == 0 and data[i] != min_element:
46+
temp[int((data[i] - min_element) / ran) - 1].append(data[i])
47+
else:
48+
temp[int((data[i] - min_element) / ran)].append(data[i])
49+
```
50+
51+
Sort each bucket individually.
52+
53+
```python
54+
for i in range(len(temp)):
55+
if len(temp[i]) != 0:
56+
temp[i].sort()
57+
```
58+
59+
Gather sorted elements to the original array.
60+
61+
```python
62+
k = 0
63+
for lst in temp:
64+
if lst:
65+
for i in lst:
66+
data[k] = i
67+
k += 1
68+
```
69+
70+
Draw the data being compared and the finalized.
71+
72+
```python
73+
draw_data(data, [LIGHT_GREEN for x in range(len(temp))])
74+
time.sleep(time_tick)
75+
return draw_data(data, [BLUE for x in range(len(data))])
76+
```
77+
78+
## Example
79+
80+
<p align="center">
81+
<img src="../images/bucket_sort.png" />
82+
</p>

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ algorithm_list = [
7575
'Quick Sort',
7676
'Counting Sort',
7777
'Radix Sort',
78-
'Heap Sort'
78+
'Heap Sort',
79+
'Bucket Sort'
7980
]
8081
```
8182

@@ -245,6 +246,7 @@ from algorithms.quickSort import quick_sort
245246
from algorithms.countingSort import counting_sort
246247
from algorithms.radixSort import radix_sort
247248
from algorithms.heapSort import heap_sort
249+
from algorithms.bucketSort import bucket_sort
248250
```
249251

250252
```python
@@ -267,6 +269,8 @@ def sort():
267269
radix_sort(data, draw_data, time_tick)
268270
elif algorithm_menu.get() == 'Heap Sort':
269271
heap_sort(data, draw_data, time_tick)
272+
elif algorithm_menu.get() == 'Bucket Sort':
273+
bucket_sort(data, len(data), draw_data, time_tick)
270274
else:
271275
pass
272276
```
@@ -293,4 +297,8 @@ GeeksforGeeks. (2021, 15 junio). Radix Sort. https://www.geeksforgeeks.org/radix
293297

294298
Heap Sort Tutorials & Notes | Algorithms. (2016, 24 abril). HackerEarth. https://www.hackerearth.com/practice/algorithms/sorting/heap-sort/tutorial/
295299

296-
GeeksforGeeks. (2020, 30 diciembre). Python Program for Heap Sort. https://www.geeksforgeeks.org/python-program-for-heap-sort/
300+
GeeksforGeeks. (2020, 30 diciembre). Python Program for Heap Sort. https://www.geeksforgeeks.org/python-program-for-heap-sort/
301+
302+
Bucket Sort Tutorials & Notes | Algorithms. (2016, 24 abril). HackerEarth. https://www.hackerearth.com/practice/algorithms/sorting/bucket-sort/tutorial/
303+
304+
GeeksforGeeks. (2021, 8 junio). Bucket Sort. https://www.geeksforgeeks.org/bucket-sort-2/
1.05 KB
Binary file not shown.

algorithms/bucketSort.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import time
2+
from colors import *
3+
4+
5+
def bucket_sort(data, n_buckets, draw_data, time_tick):
6+
max_element = max(data)
7+
min_element = min(data)
8+
ran = (max_element - min_element) / n_buckets
9+
temp = []
10+
for i in range(n_buckets):
11+
temp.append([])
12+
for i in range(len(data)):
13+
diff = (data[i] - min_element) / ran - int((data[i] - min_element) / ran)
14+
if diff == 0 and data[i] != min_element:
15+
temp[int((data[i] - min_element) / ran) - 1].append(data[i])
16+
else:
17+
temp[int((data[i] - min_element) / ran)].append(data[i])
18+
for i in range(len(temp)):
19+
if len(temp[i]) != 0:
20+
temp[i].sort()
21+
k = 0
22+
for lst in temp:
23+
if lst:
24+
for i in lst:
25+
data[k] = i
26+
k += 1
27+
draw_data(data, [LIGHT_GREEN for x in range(len(temp))])
28+
time.sleep(time_tick)
29+
return draw_data(data, [BLUE for x in range(len(data))])

images/bucket_sort.png

141 KB
Loading

main.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from algorithms.countingSort import counting_sort
1212
from algorithms.radixSort import radix_sort
1313
from algorithms.heapSort import heap_sort
14+
from algorithms.bucketSort import bucket_sort
1415

1516

1617
root = Tk()
@@ -28,7 +29,8 @@
2829
'Quick Sort',
2930
'Counting Sort',
3031
'Radix Sort',
31-
'Heap Sort'
32+
'Heap Sort',
33+
'Bucket Sort'
3234
]
3335

3436
speed_name = StringVar()
@@ -91,6 +93,8 @@ def sort():
9193
radix_sort(data, draw_data, time_tick)
9294
elif algorithm_menu.get() == 'Heap Sort':
9395
heap_sort(data, draw_data, time_tick)
96+
elif algorithm_menu.get() == 'Bucket Sort':
97+
bucket_sort(data, len(data), draw_data, time_tick)
9498
else:
9599
pass
96100

0 commit comments

Comments
 (0)