1. Gyanastra
  2. Algorithms
  3. Sorting Beyond the Lecture: Linear-Time Sorts, Lower Bound, Selection and the Full Comparison
  4. Bubble Sort (with the swapped flag)
Free lesson, no account needed 4 min read
Asked in past papers4 times
  • GATE CSE20001995
  • ISRO2017
  • UGC NET2011

Algorithms · Sorting Beyond the Lecture: Linear-Time Sorts, Lower Bound, Selection and the Full Comparison

Bubble Sort (with the swapped flag)

This chapter picks up where "Merge Sort and Quick Sort" stops. We finish the elementary sorts (bubble with the swapped flag, selection with exact counts, shell sort), then cross the wall that comparison sorting cannot cross — the Ω(nlog⁡n)\Omega(n\log n) lower bound — and go around it with counting, radix and bucket sort. Then we turn sorting inside out: instead of sorting everything, we select (min, max, second largest, kk-th smallest) with exact comparison counts. Finally, one master table for every sort, the GATE question patterns, and the traps.


Idea. Repeatedly walk the array left to right; whenever two neighbours are out of order, swap them. After one full pass the largest element has "bubbled" to the last position. After pass ii, the last ii elements are final.

The swapped flag. If a pass makes no swap, the array is already sorted — stop. This makes bubble sort Θ(n)\Theta(n) on already-sorted input.

BubbleSort(A[0..n-1]):
  for i = 0 to n-2:
      swapped = false
      for j = 0 to n-2-i:          // last i elements already final
          if A[j] > A[j+1]:
              swap(A[j], A[j+1])
              swapped = true
      if swapped == false: break   // early exit

Exact counts (worst case, reverse-sorted input).

  • Comparisons: ∑i=0n−2(n−1−i)=(n−1)+(n−2)+⋯+1=n(n−1)2\sum_{i=0}^{n-2}(n-1-i) = (n-1)+(n-2)+\cdots+1 = \dfrac{n(n-1)}{2}.
  • Swaps: every comparison causes a swap, so also n(n−1)2\dfrac{n(n-1)}{2}.

Best case (already sorted, with flag). Pass 1 does n−1n-1 comparisons, 0 swaps, then exits: Θ(n)\Theta(n) comparisons, 0 swaps. Without the flag it is still Θ(n2)\Theta(n^2) — this is a favourite GATE trap.

Stability. Only swaps adjacent elements when strictly out of order (A[j]>A[j+1]A[j] > A[j+1]), so equal elements never cross: stable. In place: yes, O(1)O(1) extra space.


Selection Sort — Exact Comparison and Swap Counts

Idea. For each position ii from 00 to n−2n-2, find the minimum of A[i..n−1]A[i..n-1] and swap it into position ii.

SelectionSort(A[0..n-1]):
  for i = 0 to n-2:
      min = i
      for j = i+1 to n-1:
          if A[j] < A[min]: min = j
      if min != i: swap(A[i], A[min])

Comparisons — always exactly the same, no best/worst difference:

∑i=0n−2(n−1−i)=n(n−1)2.\sum_{i=0}^{n-2}(n-1-i) = \frac{n(n-1)}{2}.

Swaps — at most n−1n-1 (one per outer iteration), and exactly n−1n-1 in the worst case. This is the key contrast with bubble/insertion: selection sort does Θ(n2)\Theta(n^2) comparisons but only O(n)O(n) swaps, which matters when writes are expensive.

Worked example. A=[64,25,12,22,11]A = [64, 25, 12, 22, 11], n=5n=5. Comparisons per pass: 4+3+2+1=10=5⋅424+3+2+1 = 10 = \frac{5\cdot4}{2}. ✓

Pass iiArray beforemin foundSwap?Array after
064 25 12 22 1111 (index 4)yes11 25 12 22 64
111 25 12 22 6412 (index 2)yes11 12 25 22 64
211 12 25 22 6422 (index 3)yes11 12 22 25 64
311 12 22 25 6425 (index 3)no11 12 22 25 64

Swaps = 3 (≤ n−1=4n-1 = 4). ✓

Stability. The long-distance swap can jump an equal element over its twin: [2a,2b,1][2_a, 2_b, 1] → swap 2_a with 1 → [1,2b,2a][1, 2_b, 2_a] — order of the 2's reversed. Not stable. In place: yes.


Shell Sort — the Idea

Insertion sort is fast on nearly sorted arrays (it moves an element only past elements it must pass). Shell sort exploits this: sort subsequences with a gap hh, shrinking hh to 1.

  • Pick a gap sequence h1>h2>⋯>ht=1h_1 > h_2 > \cdots > h_t = 1 (e.g. n/2,n/4,…,1n/2, n/4, \dots, 1).
  • For each gap hh, do an insertion sort on each of the hh interleaved subsequences A[i],A[i+h],A[i+2h],…A[i], A[i+h], A[i+2h], \dots
  • The final gap-1 pass is ordinary insertion sort, but on an array that is now nearly sorted, so it is cheap.

Complexity. Depends entirely on the gap sequence; the exact worst case is a research topic, so GATE does not ask for a closed form. What GATE does ask: shell sort is in place (O(1)O(1) extra space), not stable (elements with equal keys in different gap-subsequences can be reordered), and its last pass is insertion sort.


Pages 1–2 of 2