|
| 1 | +""" |
| 2 | +Algorithm: Bucket Sort |
| 3 | +Description: Sorts elements by distributing them into buckets, sorting each bucket, and then combining them |
| 4 | +Time Complexity: Worst - O(n^2) |
| 5 | +Space Complexity: O(n + k) |
| 6 | +Author: Kashyap |
| 7 | +Date: 2025-10-01 |
| 8 | +""" |
| 9 | + |
| 10 | +def insertion_sort(bucket: list): |
| 11 | + """ |
| 12 | + Sort a single bucket using Insertion Sort. |
| 13 | + |
| 14 | + We use Insertion Sort because buckets usually have few elements, |
| 15 | + making it efficient for small arrays. |
| 16 | + |
| 17 | + Args: |
| 18 | + bucket (list): List of numbers in a bucket |
| 19 | + """ |
| 20 | + for current_index in range(1, len(bucket)): |
| 21 | + current_value = bucket[current_index] |
| 22 | + position = current_index - 1 |
| 23 | + |
| 24 | + # Shift larger elements one position to the right |
| 25 | + while position >= 0 and bucket[position] > current_value: |
| 26 | + bucket[position + 1] = bucket[position] |
| 27 | + position -= 1 |
| 28 | + bucket[position + 1] = current_value |
| 29 | + |
| 30 | + |
| 31 | +def bucket_sort(array: list): |
| 32 | + """ |
| 33 | + Sort the input array using Bucket Sort. |
| 34 | +
|
| 35 | + Steps: |
| 36 | + 1. Create empty buckets. Each bucket will hold elements in a value range. |
| 37 | + 2. Distribute elements into buckets based on their value. |
| 38 | + 3. Sort each bucket individually. |
| 39 | + 4. Merge all sorted buckets back into the original array. |
| 40 | + |
| 41 | + Args: |
| 42 | + array (list): List of numbers to be sorted |
| 43 | +
|
| 44 | + Example: |
| 45 | + arr = [0.78, 0.17, 0.39, 0.26, 0.72] |
| 46 | + bucket_sort(arr) |
| 47 | + arr -> [0.17, 0.26, 0.39, 0.72, 0.78] |
| 48 | + """ |
| 49 | + n = len(array) |
| 50 | + if n == 0: |
| 51 | + return |
| 52 | + |
| 53 | + buckets = [[] for _ in range(n)] # create the n empty lists |
| 54 | + |
| 55 | + # Distribute elements into buckets |
| 56 | + for value in array: |
| 57 | + bucket_index = int(n * value) |
| 58 | + if bucket_index == n: # Edge case: value == 1 |
| 59 | + bucket_index = n - 1 |
| 60 | + buckets[bucket_index].append(value) |
| 61 | + |
| 62 | + # Sort each bucket individually using insertion sort |
| 63 | + for bucket in buckets: |
| 64 | + insertion_sort(bucket) |
| 65 | + |
| 66 | + # Merge all buckets into the original array |
| 67 | + current_index = 0 |
| 68 | + for bucket in buckets: |
| 69 | + for value in bucket: |
| 70 | + array[current_index] = value |
| 71 | + current_index += 1 |
| 72 | + |
| 73 | + |
| 74 | +# ------------------------------- |
| 75 | +# Main func and test cases |
| 76 | +# ------------------------------- |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + # Test case 1: |
| 80 | + test_array1 = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68] |
| 81 | + bucket_sort(test_array1) |
| 82 | + print("Sorted array 1:", test_array1) # [0.12, 0.17, 0.21, 0.23, 0.26, 0.39, 0.68, 0.72, 0.78, 0.94] |
| 83 | + |
| 84 | + # Test case 2: |
| 85 | + test_array2 = [0.1, 0.2, 0.3, 0.4, 0.5] |
| 86 | + bucket_sort(test_array2) |
| 87 | + print("Sorted array 2:", test_array2) # [0.1, 0.2, 0.3, 0.4, 0.5] |
| 88 | + |
| 89 | + # Test case 3: |
| 90 | + test_array3 = [0.4, 0.2, 0.4, 0.1, 0.2] |
| 91 | + bucket_sort(test_array3) |
| 92 | + print("Sorted array 3:", test_array3) # [0.1, 0.2, 0.2, 0.4, 0.4] |
| 93 | + |
| 94 | + # Test case 4: |
| 95 | + test_array4 = [] |
| 96 | + bucket_sort(test_array4) |
| 97 | + print("Sorted array 4:", test_array4) # [] |
| 98 | + |
| 99 | + # Test case 5: |
| 100 | + test_array5 = [0.5] |
| 101 | + bucket_sort(test_array5) |
| 102 | + print("Sorted array 5:", test_array5) # [0.5] |
| 103 | + |
0 commit comments