|
1 | 1 | # Bucket Sort
|
2 | 2 | '''
|
3 |
| -Bucket Sort is a sorting algorithm used to deal with input that is distributed over a range. |
4 |
| -It is a comparison sort algorithm. |
5 |
| -The elements of the list are distributed into a number of buckets(or bins).Each bucket is |
6 |
| -then sorted using any stable sorting algoritm.After sorting is complete, all the buckets are |
7 |
| -merged and we get a sorted list. |
8 |
| -Bucket Sort has a scatter-order-gather approach. |
| 3 | +Bucket Sort is a comparison sorting algorithm used to deal with input that is |
| 4 | +distributed over a range. |
| 5 | +The elements of the list are distributed into a number of buckets(or bins). |
| 6 | +Each bucket is then sorted using any stable sorting algoritm.After |
| 7 | +sorting is complete, all the buckets are merged and we get a sorted list. |
| 8 | +Bucket Sort uses a scatter-order-gather approach. |
9 | 9 | '''
|
10 | 10 |
|
11 | 11 |
|
12 | 12 | def bucket_sort(arr):
|
13 |
| - # create a list containing n empty buckets where buckets are also list |
| 13 | + # create a list containing len(arr) empty buckets where buckets are also list |
14 | 14 | bucket_list = []
|
15 | 15 | for i in enumerate(arr):
|
16 |
| - bucket_list.append([]) |
| 16 | + bucket_num = i[0] |
| 17 | + if bucket_num< len(arr): |
| 18 | + bucket_list.append([]) |
17 | 19 |
|
18 | 20 | # numerical range of each bucket
|
19 | 21 | maximum = max(arr)
|
@@ -55,14 +57,12 @@ def main():
|
55 | 57 |
|
56 | 58 |
|
57 | 59 | # Sample I/O
|
58 |
| -''' |
59 |
| -Enter sequence of comma separated numbers: |
60 |
| -6.3,12.5,7.7,2.6,1.2,4.0 |
61 |
| -
|
62 |
| -Sorted List |
63 |
| -1.2 2.6 4.0 6.3 7.7 12.5 |
| 60 | +# Enter sequence of comma separated numbers: |
| 61 | +# 6.3,12.5,7.7,2.6,1.2,4.0 |
64 | 62 |
|
65 |
| -Time Complexity - |
| 63 | +# Sorted List |
| 64 | +# 1.2 2.6 4.0 6.3 7.7 12.5 |
| 65 | +''' Time Complexity - |
66 | 66 | 1. Best case - O(n+k)
|
67 | 67 | 2. Average case - O(n+k)
|
68 | 68 | 3. Worst case - O(n^2)
|
|
0 commit comments