|
| 1 | +# Bucket Sort Algorithm |
| 2 | + |
| 3 | +## Table of Contents |
| 4 | +- [Introduction](#introduction) |
| 5 | +- [Algorithm Explanation](#algorithm-explanation) |
| 6 | +- [Pseudocode](#pseudocode) |
| 7 | +- [Time and Space Complexity](#time-and-space-complexity) |
| 8 | +- [Mermaid Diagram](#mermaid-diagram) |
| 9 | +- [Testing Results](#testing-results) |
| 10 | +- [Usage Guide](#usage-guide) |
| 11 | + |
| 12 | +## Introduction |
| 13 | +Bucket Sort is a distribution sort algorithm that works by distributing the elements |
| 14 | +of an array into a number of buckets. Each bucket is then sorted individually, either |
| 15 | +using a different sorting algorithm or by recursively applying the bucket sort algorithm. |
| 16 | + |
| 17 | +## Algorithm Explanation |
| 18 | +1. Determine the minimum and maximum values in the array. |
| 19 | +2. Initialize a number of empty buckets. |
| 20 | +3. Distribute the elements of the array into the buckets based on a calculated index. |
| 21 | +4. Sort each bucket individually. |
| 22 | +5. Concatenate the sorted buckets to form the final sorted array. |
| 23 | + |
| 24 | +## Pseudocode |
| 25 | +``` |
| 26 | +procedure bucketSort(A: list of sortable items) |
| 27 | + n := length(A) |
| 28 | + if n = 0 then |
| 29 | + return A |
| 30 | + end if |
| 31 | + min_value := min(A) |
| 32 | + max_value := max(A) |
| 33 | + bucket_count := n |
| 34 | + buckets := array of empty lists of size bucket_count |
| 35 | + for i := 0 to n-1 do |
| 36 | + index := (A[i] - min_value) / (max_value - min_value + 1) * bucket_count |
| 37 | + buckets[index].append(A[i]) |
| 38 | + end for |
| 39 | + sorted_array := empty list |
| 40 | + for each bucket in buckets do |
| 41 | + sorted_array.extend(sort(bucket)) |
| 42 | + end for |
| 43 | + return sorted_array |
| 44 | +end procedure |
| 45 | +``` |
| 46 | + |
| 47 | +## Time and Space Complexity |
| 48 | +- **Best Case:** O(n + k) where k is the number of buckets |
| 49 | +- **Average Case:** O(n + k) |
| 50 | +- **Worst Case:** O(n^2) when all elements are placed in a single bucket |
| 51 | +- **Space Complexity:** O(n + k) |
| 52 | + |
| 53 | +## Mermaid Diagram |
| 54 | +```mermaid |
| 55 | +graph TD |
| 56 | + A[Start] --> B[Determine min and max values] |
| 57 | + B --> C[Initialize empty buckets] |
| 58 | + C --> D[Distribute elements into buckets] |
| 59 | + D --> E[Sort each bucket] |
| 60 | + E --> F[Concatenate sorted buckets] |
| 61 | + F --> G[End] |
| 62 | +``` |
| 63 | + |
| 64 | +## Testing Results |
| 65 | +| Test Case | Input | Expected Output | Actual Output | |
| 66 | +|--------------------------|----------------------|-----------------------|-----------------------| |
| 67 | +| Empty List | [] | [] | [] | |
| 68 | +| Single Element | [1] | [1] | [1] | |
| 69 | +| Already Sorted | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] | |
| 70 | +| Reverse Sorted | [5, 4, 3, 2, 1] | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] | |
| 71 | +| Duplicates | [3, 1, 2, 3, 1] | [1, 1, 2, 3, 3] | [1, 1, 2, 3, 3] | |
| 72 | +| Large Dataset | [1000, 999, ..., 1] | [1, 2, ..., 1000] | [1, 2, ..., 1000] | |
| 73 | + |
| 74 | +## Usage Guide |
| 75 | +To use the Bucket Sort algorithm, follow these steps: |
| 76 | +1. Import the `bucket_sort` function from the `bucket_sort` module. |
| 77 | +2. Pass the list of integers to be sorted as an argument to the `bucket_sort` function. |
| 78 | +3. The function will return the sorted list of integers. |
| 79 | + |
| 80 | +```python |
| 81 | +from bucket_sort import bucket_sort |
| 82 | + |
| 83 | +arr = [64, 34, 25, 12, 22, 11, 90] |
| 84 | +sorted_arr = bucket_sort(arr) |
| 85 | +print("Sorted array is:", sorted_arr) |
| 86 | +``` |
0 commit comments