MCS-208 · Unit 9

Searching and Sorting Techniques

Section 4 of 9

Block 3 · Unit 9Searching and Sorting Techniques8 min · Section 4 of 9

9.4Insertion Sort and Bubble Sort

Covers IGNOU MCS-208 §9.5, 9.5.1, 9.5.2

Sorting puts records in order of their keys. This page introduces two simple methods that are easy to understand and good for small or nearly sorted data, and the important idea of a stable sort.

Internal sorting and stability

In internal sorting all records fit in main memory; external sorting (the last section) handles files that do not.

Stability matters when records are sorted on one key after another (for example by name, then by city): a stable second sort keeps the first order among equal cities.

Insertion sort

For j=1,…,n−1j = 1, \dots, n - 1: take x=a[j]x = a[j]; shift every element of the sorted part a[0..j−1]a[0..j-1] that is larger than xx one place right; put xx into the gap. (Unit 1 analyses it exactly.)

  • Best case (already sorted): n−1n - 1 comparisons, O(n)O(n).
  • Worst case (reverse order): n(n−1)2\frac{n(n-1)}{2} comparisons, O(n2)O(n^2); average also O(n2)O(n^2).
  • Stable (it shifts only strictly larger elements), in place.

Bubble sort

In each pass, compare neighbouring elements from the end of the array towards the front and swap them if they are out of order; the smallest remaining element “bubbles up” to the front. After pass ii the first ii elements are in their final places.

  • Comparisons: (n−1)+(n−2)+⋯+1=n(n−1)2(n - 1) + (n - 2) + \dots + 1 = \frac{n(n-1)}{2}, i.e. O(n2)O(n^2).
  • With a flag that stops when a pass makes no swap, an already sorted array needs only one pass: O(n)O(n).
  • Stable only if equal neighbours are not swapped.

Key takeaways

  • Internal sorting: all data in memory. Stable: equal keys keep their order.
  • Insertion sort: insert each element into the sorted prefix; O(n)O(n) best, O(n2)O(n^2) worst/average; stable.
  • Bubble sort: swap out-of-order neighbours in passes; O(n2)O(n^2); O(n)O(n) best with an early-exit flag.
  • Use <, not <=, so equal keys are not swapped.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the array after the first pass of insertion sort (inserting the second element) on 7 4 9 1?

Show answer

Answer: B. 4 7 9 1

Only 4 is inserted into the sorted prefix [7], giving 4 7; the rest is unchanged.

Question 2 · multiple choice

How many comparisons does bubble sort (without an early exit) make on 6 elements?

Show answer

Answer: D. 15

5+4+3+2+1=15=6⋅5/25 + 4 + 3 + 2 + 1 = 15 = 6 \cdot 5 / 2.

Question 3 · multiple choice

Which input gives insertion sort its best case?

Show answer

Answer: A. An already sorted array

Each new element is compared once and stays in place: n−1n - 1 comparisons.

Question 4 · multiple choice

Records (2, a), (1, b), (2, c) are sorted by the number with a stable sort. What is the result?

Show answer

Answer: B. (1, b), (2, a), (2, c)

Stable means (2, a) stays before (2, c), as in the input. “Either” is true only for an unstable sort.

Question 5 · multiple choice

What does this program print?

C · q9-bubble.c
#include <stdio.h>

int main(void)
{
    int a[] = {5, 1, 4, 2}, n = 4, passes = 0;
    for (int i = 0; i < n - 1; i++) {
        int swapped = 0;
        for (int j = n - 1; j > i; j--)
            if (a[j] < a[j - 1]) {
                int t = a[j]; a[j] = a[j - 1]; a[j - 1] = t;
                swapped = 1;
            }
        passes++;
        if (!swapped) break;
    }
    printf("%d %d %d %d after %d passes\n", a[0], a[1], a[2], a[3], passes);
    return 0;
}
Show answer

Answer: C. 1 2 4 5 after 3 passes

Pass 1 gives 1 5 2 4; pass 2 gives 1 2 5 4; pass 3 gives 1 2 4 5 (a swap happened, so no early exit); the loop then ends because ii reaches n−1n - 1.

Question 6 · short answer

Sort 34 8 64 51 32 21 using insertion sort, showing the array after each insertion, and state the number of comparisons in the worst case for 6 elements.

Show worked answer

Insert 8: 8 34 64 51 32 21. Insert 64: 8 34 64 51 32 21. Insert 51: 8 34 51 64 32 21. Insert 32: 8 32 34 51 64 21. Insert 21: 8 21 32 34 51 64. Worst case (reverse order): 1+2+3+4+5=15=n(n−1)/21 + 2 + 3 + 4 + 5 = 15 = n(n-1)/2 comparisons, so the method is O(n2)O(n^2).

↑ ↓ move↵ openTab switch type