1
- #!/usr/bin/env python
2
-
3
- """Illustrate how to implement bucket sort algorithm."""
4
-
5
- # Author: OMKAR PATHAK
6
- # This program will illustrate how to implement bucket sort algorithm
7
-
8
- # Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works
9
- # by distributing the elements of an array into a number of buckets.
10
- # Each bucket is then sorted individually, either using a different sorting
11
- # algorithm, or by recursively applying the bucket sorting algorithm. It is a
12
- # distribution sort, and is a cousin of radix sort in the most to least
13
- # significant digit flavour.
14
- # Bucket sort is a generalization of pigeonhole sort. Bucket sort can be
15
- # implemented with comparisons and therefore can also be considered a
16
- # comparison sort algorithm. The computational complexity estimates involve the
17
- # number of buckets.
18
-
19
- # Time Complexity of Solution:
20
- # Worst case scenario occurs when all the elements are placed in a single bucket. The
21
- # overall performance would then be dominated by the algorithm used to sort each bucket.
22
- # In this case, O(n log n), because of TimSort
23
- #
24
- # Average Case O(n + (n^2)/k + k), where k is the number of buckets
25
- #
26
- # If k = O(n), time complexity is O(n)
1
+ #!/usr/bin/env python3
2
+ """
3
+ Illustrate how to implement bucket sort algorithm.
27
4
5
+ Author: OMKAR PATHAK
6
+ This program will illustrate how to implement bucket sort algorithm
7
+
8
+ Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works
9
+ by distributing the elements of an array into a number of buckets.
10
+ Each bucket is then sorted individually, either using a different sorting
11
+ algorithm, or by recursively applying the bucket sorting algorithm. It is a
12
+ distribution sort, and is a cousin of radix sort in the most to least
13
+ significant digit flavour.
14
+ Bucket sort is a generalization of pigeonhole sort. Bucket sort can be
15
+ implemented with comparisons and therefore can also be considered a
16
+ comparison sort algorithm. The computational complexity estimates involve the
17
+ number of buckets.
18
+
19
+ Time Complexity of Solution:
20
+ Worst case scenario occurs when all the elements are placed in a single bucket.
21
+ The overall performance would then be dominated by the algorithm used to sort each
22
+ bucket. In this case, O(n log n), because of TimSort
23
+
24
+ Average Case O(n + (n^2)/k + k), where k is the number of buckets
25
+
26
+ If k = O(n), time complexity is O(n)
27
+
28
+ Source: https://en.wikipedia.org/wiki/Bucket_sort
29
+ """
28
30
DEFAULT_BUCKET_SIZE = 5
29
31
30
32
31
- def bucket_sort (my_list , bucket_size = DEFAULT_BUCKET_SIZE ):
33
+ def bucket_sort (my_list : list , bucket_size : int = DEFAULT_BUCKET_SIZE ) -> list :
34
+ """
35
+ >>> data = [-1, 2, -5, 0]
36
+ >>> bucket_sort(data) == sorted(data)
37
+ True
38
+
39
+ >>> data = [9, 8, 7, 6, -12]
40
+ >>> bucket_sort(data) == sorted(data)
41
+ True
42
+
43
+ >>> data = [.4, 1.2, .1, .2, -.9]
44
+ >>> bucket_sort(data) == sorted(data)
45
+ True
46
+
47
+ >>> bucket_sort([])
48
+ Traceback (most recent call last):
49
+ ...
50
+ Exception: Please add some elements in the array.
51
+ """
32
52
if len (my_list ) == 0 :
33
53
raise Exception ("Please add some elements in the array." )
34
54
@@ -40,11 +60,10 @@ def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE):
40
60
buckets [int ((my_list [i ] - min_value ) // bucket_size )].append (my_list [i ])
41
61
42
62
return sorted (
43
- [ buckets [i ][j ] for i in range (len (buckets )) for j in range (len (buckets [i ]))]
63
+ buckets [i ][j ] for i in range (len (buckets )) for j in range (len (buckets [i ]))
44
64
)
45
65
46
66
47
67
if __name__ == "__main__" :
48
- user_input = input ("Enter numbers separated by a comma:" ).strip ()
49
- unsorted = [float (n ) for n in user_input .split ("," ) if len (user_input ) > 0 ]
50
- print (bucket_sort (unsorted ))
68
+ assert bucket_sort ([4 , 5 , 3 , 2 , 1 ]) == [1 , 2 , 3 , 4 , 5 ]
69
+ assert bucket_sort ([0 , 1 , - 10 , 15 , 2 , - 2 ]) == [- 10 , - 2 , 0 , 1 , 2 , 15 ]
0 commit comments