
Bucket Sort is a sorting algorithm that distributes elements into several groups (buckets) based on their values. Each bucket is then sorted individually, and the results are merged to produce a sorted array. It is most effective when data is uniformly distributed over a range and is particularly suitable for sorting floating-point numbers.
Bucket 0: [0.12] Bucket 1: [0.17, 0.21, 0.23] Bucket 2: [0.26] Bucket 3: [0.39] Bucket 6: [0.68] Bucket 7: [0.72, 0.78] Bucket 9: [0.94]import java.util.ArrayList;
import java.util.Collections;
public class BucketSort {
public static void bucketSort(float[] arr) {
int n = arr.length;
// 1. Initialize buckets
ArrayList<Float>[] buckets = new ArrayList[n];
for (int i = 0; i < n; i++) {
buckets[i] = new ArrayList<>();
}
// 2. Distribute elements into buckets
for (float num : arr) {
int bucketIndex = (int) (num * n); // Calculate bucket index
buckets[bucketIndex].add(num);
}
// 3. Sort each bucket
for (ArrayList<Float> bucket : buckets) {
Collections.sort(bucket);
}
// 4. Merge buckets
int index = 0;
for (ArrayList<Float> bucket : buckets) {
for (float num : bucket) {
arr[index++] = num;
}
}
}
public static void main(String[] args) {
float[] arr = {0.78f, 0.17f, 0.39f, 0.26f, 0.72f, 0.94f, 0.21f, 0.12f, 0.23f, 0.68f};
System.out.println("Original array:");
for (float num : arr) {
System.out.print(num + " ");
}
bucketSort(arr);
System.out.println("\nSorted array:");
for (float num : arr) {
System.out.print(num + " ");
}
}
}
def bucket_sort(arr):
n = len(arr)
if n <= 0:
return arr
# 1. Create buckets
buckets = [[] for _ in range(n)]
# 2. Distribute elements into buckets
for num in arr:
bucket_index = int(num * n) # Calculate bucket index
buckets[bucket_index].append(num)
# 3. Sort each bucket
for bucket in buckets:
bucket.sort()
# 4. Merge buckets
sorted_array = []
for bucket in buckets:
sorted_array.extend(bucket)
return sorted_array
# Example usage
if __name__ == "__main__":
arr = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68]
print("Original array:", arr)
sorted_arr = bucket_sort(arr)
print("Sorted array:", sorted_arr)
Bucket Sort is highly efficient for specific scenarios but requires careful consideration of data distribution and bucket configuration for optimal performance.
When analyzing a stock, one of the first financial indicators you’ll encounter is EPS, or Earnings Per Share. It’s one… Read More
When you look at a stock’s profile on a financial website, one of the first things you’ll see is its… Read More
In the world of open-source software, simplicity and flexibility are often just as important as legal protection. That’s why the… Read More
If you want your software to be open source, but still compatible with commercial use—and not as restrictive as the… Read More
When it comes to open-source software, developers and businesses alike need licenses that balance freedom, legal clarity, and long-term security.… Read More
If you’re working on open-source projects or choosing third-party libraries for your software, understanding software licenses is essential. Among the… Read More