DSA Lab Manual (Bucket Sort)
DSA Lab Manual (Bucket Sort)
Objective: To understand and implement the Bucket Sort algorithm using C++ programming
language.
Theory: Bucket Sort is a sorting algorithm that distributes elements of an array into several
buckets. Each bucket is then sorted individually, either using a different sorting algorithm or by
recursively applying Bucket Sort. This algorithm is particularly useful when the input is
uniformly distributed over a range.
Algorithm Steps:
Advantages:
Disadvantages:
Applications:
#include <iostream>
#include <vector>
#include <algorithm> // for sort()
using namespace std;
int main() {
// Sample array of floating-point numbers in range [0, 1)
vector<float> arr = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
return 0;
}
Practical Observations:
Viva Questions:
Conclusion: Bucket Sort is an efficient sorting algorithm for a specific range of inputs,
especially when the data is uniformly distributed. The implementation requires careful
consideration of bucket size and sorting methodology within buckets to achieve optimal
performance.
Copy
[0] → [], [1] → [], [2] → [], [3] → [], [4] → [], [5] → [].
For each element, compute the bucket index using bucketIndex = n * arr[i] (truncated to integer).
Bucket 0: [0.1234]
Bucket 1: []
Bucket 2: [0.3434]
Bucket 4: []
Bucket 5: [0.897]
Note:
Key Observations:
The code sorts every bucket, even if it contains a single element or is already sorted.
Order of Concatenation:
1. Bucket 0: 0.1234
2. Bucket 1: [] (skipped)
3. Bucket 2: 0.3434
5. Bucket 4: [] (skipped)
6. Bucket 5: 0.897
Final Sorted Array:
[0.1234, 0.3434, 0.565, 0.656, 0.665, 0.897]
o If elements are clustered in a few buckets (e.g., Bucket 3 in this example), sorting those
buckets dominates the runtime.
2. Edge Cases:
o The input array here avoids this issue by having elements strictly less than 1.
3. Time Complexity:
o Best Case: O(n) (if all elements land in one bucket and are already sorted).
o Average Case: O (n + k), where k is the cost of sorting buckets (often approximated
as O(n) for uniformly distributed data).
o Worst Case: O(n²) (if all elements land in one bucket and require sorting).
Final Output
The algorithm successfully sorts the input array using bucket sort.
Sorted Array:
[0.1234, 0.3434, 0.565, 0.656, 0.665, 0.897]
2. Sorting algorithm used for individual buckets (e.g., O (k log k) vs. O(k2)).
n = number of elements.
Sorting each bucket uses an O(ki log ki) algorithm (e.g., std::sort).
Step 1: Distribute Elements into Buckets
Time: O(n)
Each element is placed into a bucket using O(1) calculations.
Time: O(n)
Traverse all buckets and merge them.
Worst Case O (n log n) All elements in one bucket (with O(k log k) sorting).
Worst Case* O(n2) All elements in one bucket (with O(k2) sorting, e.g., insertion sort).
Key Observations
1. Uniform Distribution:
2. Non-Uniform Distribution:
o With O (k log k) sorting (e.g., merge sort), it remains O(n log n).
3. Space Complexity:
For n buckets, the probability of an element falling into any bucket is p=1/n.