1. Single Bucket with Comparison Sort (Brute Force)
Intuition
Place all floating numbers into a single list and apply insertion sort or bubble sort to sort in O(n2) worst case.
When to apply: Use as a baseline comparison before distributing into multiple buckets.
Single Bucket Insertion Sort Trace
2 of 2 questions left todayArray of 6 floats in [0, 1): [0
Array of 6 floats in [0, 1): [0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434].
import java.util.*; public class Main { public static class Solution { public double[] bucketSort(double[] arr) { int n = arr.length; for (int i = 1; i < n; i++) { double key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } return arr; } } public static void main(String[] args) { Solution sol = new Solution(); double[] arr = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434}; System.out.println("Sorted: " + Arrays.toString(sol.bucketSort(arr))); }}Time & Space Complexity
- Time complexity:
O(n^2)— Quadratic comparison sort over all n elements. - Space complexity:
O(1) auxiliary— O(1) auxiliary memory.
Why Brute Force Doesn't Scale:
Global comparison sorts require O(N log N) or O(N2) comparisons.
For 100,000 uniform floats, Bucket Sort performs ~100,000 bucket inserts and completes in 10ms, outperforming QuickSort.