3.6.8 · HinglishSorting & Searching

Bucket sort — uniform distributions

1,477 words7 min readRead in English

3.6.8 · Coding › Sorting & Searching


WHAT it is

Woh assumption jo ise fast banati hai: inputs ek known interval par uniform distribution se drawn hain, isliye buckets evenly fill hote hain.


WHY it works — expected time scratch se derive karo

Hume expected total cost chahiye. Scatter, concatenation, aur bucket creation — teeno hain. Risky part sirf buckets ko sort karna hai, kyunki size ke bucket par insertion sort leta hai.

Maano = bucket mein girne wale elements ki sankhya. Total sort cost:

Expectation lo:

Ab humein chahiye. elements mein se har ek independently probability se bucket mein girta hai (uniform input → har bucket mein equal chance). Toh .

Identity use karo:

plug karo:

Yahi toh punchline hai: ek constant hai (), ke saath grow nahi karta. Toh:


Figure — Bucket sort — uniform distributions

HOW to implement it

def bucket_sort(a):           # a: list of floats in [0, 1)
    n = len(a)
    buckets = [[] for _ in range(n)]      # n empty buckets
    for x in a:
        buckets[int(n * x)].append(x)     # scatter: index = floor(n*x)
    out = []
    for b in buckets:
        b.sort()                          # insertion sort in practice
        out.extend(b)                     # concatenate in order
    return out

int(n*x) kyun? ko se stretch karne par value directly ek bucket address par map ho jaati hai, isliye equal-width slots equal value-ranges ke correspond karte hain.


Jab yeh FAIL hota hai (steel-man)


Recall Feynman: ek 12-saal ke bachche ko samjhao

Socho 100 bacche hain, har ek ke paas 0 se 99 tak koi number likha card hai, aur tumhe unhe smallest se largest mein line mein lagana hai. Cards ek-ek karke compare karne ki jagah, tum 100 labeled boxes rakh do (0,1,2,...). Har bachcha apna card us box mein daalta hai jo uske number ke pehle digit se match kare. Kyunki numbers evenly spread hain, almost har box mein sirf ek card aata hai. Phir boxes ko left se right padhte jao — ho gaya! Trick yeh hai: number khud batata hai kahan jaana hai, isliye sort almost karna hi nahi padta.


Recall checkpoint

Bucket sort: kaun si input distribution expected deti hai?
Ek known range par uniform distribution (taaki buckets evenly fill hon).
Bucket sort: buckets ke saath value ke liye bucket index ka formula?
.
Bucket sort: worst-case time aur kab hota hai?
, jab saare elements ek bucket mein girte hain (skewed/adversarial input).
Comparison-sort lower bound bucket sort par kyun apply nahi hota?
Scatter step value ko address ki tarah use karta hai (comparisons nahi), isliye yeh comparison sort nahi hai.
ke liye, kya hai?
(ek constant ), isliye total sort work hai.
, variance aur mean ko kaun si identity link karti hai?
.
Roughly kitne buckets use karne chahiye, aur kyun?
— expected element ke hisaab se ek; bahut kam se collisions hote hain, bahut zyada se empty buckets par time waste hota hai.
ki jagah mein values ko bucket-sort kaise karte hain?
Index (pehle normalize karo).
Kya bucket sort stable hai?
Haan, agar har bucket ka internal sort stable ho aur elements insertion order maintain karein.

Connections

Concept Map

enables even fill

index = floor n times x

sort each bucket

concatenate in order

count per bucket

E X2 = Var + mean squared

constant per bucket

not comparison-based

beats

Uniform input in 0,1

Scatter into n buckets

About one item per bucket

Insertion sort

Sorted array

n_i ~ Binomial n, 1/n

E of n_i squared = 2 - 1/n

Expected time O n

Omega n log n does not apply