datarekha

Bubble, Insertion & Selection Sort

The three quadratic sorts GATE traces pass-by-pass — bubble bubbles, selection picks the min, insertion grows a sorted prefix. Know their passes, stability, and Big-O cold.

9 min read Intermediate GATE DA Lesson 54 of 122

What you'll learn

  • Bubble sort swaps adjacent out-of-order pairs; the largest element bubbles to the end each pass
  • Selection sort swaps the minimum of the unsorted part into place: always Theta(n^2), at most n-1 swaps
  • Insertion sort grows a sorted prefix and is O(n) on nearly-sorted input
  • Stability: bubble and insertion are stable, selection is not

Before you start

Three classic sorts are all O(n²), yet GATE keeps testing them because each moves elements by a different rule — and a single pass of one looks nothing like a single pass of another. The exam shows you an array after k passes and asks which algorithm produced it, or how many swaps it took. Win those marks by knowing each algorithm’s per-pass fingerprint. The payoff is broader too: the stability and in-place tradeoffs you meet here are exactly what you weigh when picking a sort key in pandas or a database ORDER BY on real data.

The three rules

  • Bubble sort — walk the array, swapping every adjacent pair that is out of order. Each full pass drags the largest remaining element to the end (it “bubbles up”). One element settles at the back per pass.
  • Selection sort — find the minimum of the unsorted part and swap it into the next front slot. One element settles at the front per pass, using at most one swap. It always scans the whole unsorted region, so it is Θ(n²) even on an already-sorted array.
  • Insertion sort — keep the left portion sorted; take each next element and slide it left into its place. On nearly-sorted input each element barely moves, giving a O(n) best case.

Race them and watch the fingerprints: bubble and insertion freeze instantly on sorted input, while selection grinds through every comparison regardless.

One pass of each on [4, 3, 2, 1, 5]Bubble[4, 3, 2, 1, 5][3, 2, 1, 4, 5]4 bubbles rightSelection[4, 3, 2, 1, 5][1, 3, 2, 4, 5]min 1 to frontInsertion[4, 3, 2, 1, 5][3, 4, 2, 1, 5]insert 3 before 4Same array, three different first passes — that is the GATE trap.
Each sort’s first pass on the same input is distinct: largest-to-back, min-to-front, prefix-insert.
AlgorithmComparisons (worst)Swaps (worst)Best caseStable?
BubbleO(n²)O(n²)O(n) — already sortedYes
InsertionO(n²)O(n²)O(n) — nearly sortedYes
SelectionO(n²)O(n) — at most n−1Θ(n²) — always scans fullyNo

How GATE asks this

The 2024 paper gave the array [4, 3, 2, 1, 5] and asked which of bubble, insertion, selection sorts it into ascending order in exactly two passes — a pure pass-tracing MCQ. The companion NAT style hands you a small array and asks for the array state after k passes or the total number of swaps. Both reward the same skill: simulate one algorithm’s passes faithfully without confusing it with another’s.

Worked example — sorted in exactly two passes (2024)

Which of bubble, insertion, or selection sort sorts [4, 3, 2, 1, 5] into ascending order in exactly two passes? Answer: selection sort only.

Selection — pass 1 finds the minimum 1 (at index 3) and swaps it to the front; pass 2 finds the next minimum 2 and swaps it into place:

start:        [4, 3, 2, 1, 5]
pass 1: min = 1 -> swap to front  ->  [1, 3, 2, 4, 5]
pass 2: min of rest = 2 -> in place ->  [1, 2, 3, 4, 5]   ✓ sorted

Bubble drags only the largest to the back each pass, so two passes is not enough on this reversed prefix:

start:   [4, 3, 2, 1, 5]
pass 1:  [3, 2, 1, 4, 5]      (4 bubbled to the back)
pass 2:  [2, 1, 3, 4, 5]      still unsorted — 2, 1 out of order

Insertion grows a sorted prefix one slot per pass, so after two passes only the first three entries are ordered:

start:   [4, 3, 2, 1, 5]
pass 1:  [3, 4, 2, 1, 5]      (sorted prefix [3,4])
pass 2:  [2, 3, 4, 1, 5]      still unsorted — 1 not yet inserted

Only selection finishes. The reason is structural: this input puts the two smallest values at the back, and selection is the only one of the three that reaches to the back to pull a minimum forward each pass.

Quick check

Quick check

0/6
Q1Bubble sort runs on [5, 1, 4, 2, 8]. What is the array after the FIRST complete pass? Enter the third element (index 2, 0-based). (integer)numerical answer — type a number
Q2Selection sort runs on [4, 3, 2, 1, 5]. What is the array after exactly TWO passes? Enter the second element (index 1). (integer)numerical answer — type a number
Q3How many element comparisons does selection sort make on ANY array of 5 elements (e.g. an already-sorted one)? (integer)numerical answer — type a number
Q4Which of these three sorts are STABLE (equal keys keep their original relative order)? (select all that apply)select all that apply
Q5Which statements about best/worst-case behavior are TRUE? (select all that apply)select all that apply
Q6An array is reverse-sorted: [5, 4, 3, 2, 1]. Which sort performs the FEWEST swaps to sort it?

Practice this in an interview

All questions

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Explore further

Cheat sheets

Related lessons

Skip to content