3.6.1 · HinglishSorting & Searching

Bubble sort, selection sort, insertion sort — O(n²), when insertion sort wins

1,949 words9 min readRead in English

3.6.1 · Coding › Sorting & Searching


YE SAB KYU HAIN? (Scratch se derivation)

HUM KYA COUNT KARTE HAIN: comparisons ki sankhya + element moves ki sankhya, input size ke function ke roop mein.

Selection sort — exact count (hamesha same)

YE KAISE CHALTA HAI: har position ke liye se tak, baaki scan karo minimum dhundhne ke liye.

  • Pass : elements se compare karo.
  • Pass : elements se compare karo.
  • … last pass tak: comparison.

Total comparisons:

Ye sum kyun? Gauss trick: pehle aur aakhri term ko pair karo , doosra aur doosra-aakhri … aisi pairs hain jo har ek tak jodi jaati hain, jisse milta hai.

Kyunki scan kabhi jaldi nahi rukti, best, worst, aur average teeno cases mein. Swaps, lekin, zyada se zyada hain (ek per pass) — selection sort writes kam karta hai.

Bubble sort — worst case

KAISE: array mein baar baar pass karo adjacent inversions ko swap karte hue. Pass ke baad, sabse bade elements end mein lock ho jaate hain.

Worst case (reverse-sorted): pass mein comparisons hote hain:

"Kya maine swap kiya?" flag ke saath, best case (pehle se sorted) = ==ek pass, comparisons, == — lekin worst case mein bahut saare swaps karta hai ().

Insertion sort — best vs worst

KAISE: sorted prefix A[0..i-1] rakho. A[i] lo, bade elements ko right shift karo, use drop karo.

  • Best case (pehle se sorted): har naya element apne left neighbour se hai, toh inner loop turant ruk jaata hai → comparison per element → comparisons → ====.
  • Worst case (reverse-sorted): element apne saare predecessors ko cross karta hai:

Moves ki sankhya array mein inversions ki sankhya ke barabar hoti hai — yahi woh gehri wajah hai ki insertion sort nearly-sorted data par itna fast hai.

Figure — Bubble sort, selection sort, insertion sort — O(n²), when insertion sort wins

Key insight: inversions


Worked examples



Recall Feynman: 12-saal ke bachche ko samjhao

Socho ki tum playing cards ko haath mein sort kar rahe ho.

  • Insertion: tum cards ek ek karke uthate ho aur har ek ko sahi jagah slide karte ho un cards ke beech jo tum pehle se pakde ho. Agar wo almost order mein the, toh tum muskil se kuch move karte ho — super quick!
  • Selection: tum table par saare cards dekhte ho, sabse chhota dhundhte ho, rakhte ho, phir baaki sabko phir se dekhte ho, agla sabse chhota dhundhte ho… Tum hamesha sab kuch dekhte ho, chahe wo pehle se sorted hi kyun na hon.
  • Bubble: tum kisi bhi do neighbours ko jo galat order mein hain swap karte rehte ho, left se right sweep karte hue, jab tak kuch swap karne ki zaroorat na ho. Bade cards dheere dheere end par "float" karte hain. Trick: insertion sort ek achhe tarike se lazy hai — jab card pehle se sahi jagah par ho tab wo usi waqt ruk jaata hai. Isliye ye jeet jaata hai jab cheezein almost tidy hoti hain.

#flashcards/coding

Why is selection sort always even on a sorted array?
Iska inner scan minimum dhundhne ke liye kabhi jaldi nahi rukta — ye hamesha comparisons karta hai chahe input ka order kuch bhi ho.
What is the best-case time of insertion sort and when does it occur?
, jab array pehle se (almost) sorted ho — har element sirf apne left neighbour ko check karta hai aur ruk jaata hai.
Insertion sort's number of moves equals what quantity?
Array mein inversions ki sankhya (out-of-order pairs).
Which two of the three sorts are naturally stable?
Bubble aur insertion (ye adjacent elements shift karte hain); selection by default stable nahi hai.
Which sort minimises the number of writes/swaps, and why might that matter?
Selection sort — sirf swaps. Useful jab writes expensive hoon (jaise flash memory wear).
When does insertion sort beat bubble and selection in practice?
Chhote ya nearly-sorted arrays par — ye adaptive hai, isliye libraries ise faster sorts ke andar tiny sub-arrays ke liye use karti hain.
Derive selection sort's comparison count.
Gauss pairing se.
What single optimisation makes bubble sort in the best case?
Ek "swapped?" flag: agar ek poora pass koi swap nahi karta, array sorted hai — ruk jao.
Reverse-sorted input: how many comparisons does insertion sort do?
, iska worst case .
Why is bubble sort rarely used despite the same Big-O as insertion?
Ye average par kahin zyada swaps karta hai aur koi real advantage nahi hai; insertion ke better constants hain aur ye standard small-array sort hai.

Connections

  • Merge Sort divide-and-conquer; chhote chunks ke liye insertion sort use karta hai.
  • Quicksort / Introsort — size threshold ke neeche insertion sort par switch karte hain.
  • Timsort — real-world hybrid jo insertion sort + merge par bana hai, existing runs exploit karta hai.
  • Inversions and Counting — "kitna unsorted" hai array ka formal measure.
  • Big-O Notation — kyun worst-case ke aage best/average/worst sab matter karte hain.
  • Stability in Sorting — equal keys ka order preserve karna.
  • Lower Bound for Comparison Sorts barrier jo ye quadratic sorts nahi tod paate.

Concept Map

includes

includes

includes

swaps adjacent pairs

moves equal

few when

makes fast

early stop gives

gives Bubble

uses

never stops early

only n-1 swaps

Comparison sort O n2

Bubble sort

Selection sort

Insertion sort

Number of inversions

Swap flag optimisation

Nearly-sorted data

Best case O n

Minimises writes n-1