We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5a7df75 commit 0ed6aa1Copy full SHA for 0ed6aa1
Data Structures and Algorithms/bucket_sort.py
@@ -0,0 +1,29 @@
1
+import random
2
+
3
+def bucketSort(array):
4
+ largest = max(array)
5
+ length = len(array)
6
+ size = largest/length
7
8
+ # Create Buckets
9
+ buckets = [[] for i in range(length)]
10
11
+ # Bucket Sorting
12
+ for i in range(length):
13
+ index = int(array[i]/size)
14
+ if index != length:
15
+ buckets[index].append(array[i])
16
+ else:
17
+ buckets[length - 1].append(array[i])
18
19
+ # Sorting Individual Buckets
20
+ for i in range(len(array)):
21
+ buckets[i] = sorted(buckets[i])
22
23
24
+ # Flattening the Array
25
+ result = []
26
27
+ result = result + buckets[i]
28
29
+ return result
0 commit comments