Searching and sorting are the standard examples for learning analysis. We count comparisons for each algorithm and give best, worst and average cases.

Sequential (linear) search looks at the elements one by one until the key is found or the list ends. In the best case the key is the first element: 1 comparison, Ω(1). In the worst case it is the last element or absent: n comparisons, O(n). In the average case, if the key is equally likely to be at any position, the expected number of comparisons is (1 + 2 + ... + n)/n = (n + 1)/2, which is Θ(n). Space is O(1). It needs no sorting but is slow for large lists.

Bubble Sort

Bubble sort repeatedly compares adjacent elements and swaps them if they are out of order, so that after pass i the i-th largest element has "bubbled" to its final place. For n elements it needs n − 1 passes, and pass i makes n − i comparisons, so the total is (n − 1) + (n − 2) + ... + 1 = n(n − 1)/2 comparisons, which is Θ(n²) in every case. With a flag that stops when a pass makes no swap, the best case (already sorted) becomes n − 1 comparisons, Θ(n). Worst case swaps also reach n(n − 1)/2. It is stable and in place.

Selection Sort

Selection sort finds the smallest element of the unsorted part and swaps it to the front. Pass i scans n − i elements, so the number of comparisons is again n(n − 1)/2 for every input, Θ(n²), but the number of swaps is at most n − 1, which is useful when writing is expensive. It is in place but not stable in its usual form.

Insertion Sort

Insertion sort builds a sorted prefix by taking the next element and inserting it into its right place, shifting larger elements right, exactly like arranging playing cards in the hand. In the best case (sorted input) each element needs one comparison: n − 1 comparisons, Θ(n). In the worst case (reverse sorted) element i is compared with all i − 1 earlier ones: 1 + 2 + ... + (n − 1) = n(n − 1)/2, Θ(n²). The average case is about n²/4 comparisons, still Θ(n²). It is stable, in place and excellent for small or nearly sorted data.

Definition: Stable sort. A sorting algorithm that keeps equal elements in the same relative order as in the input.

Table 1.3: Comparison of the simple sorts

Algorithm Best Worst Extra space
Bubble (with flag) n n² O(1)
Selection n² n² O(1)
Insertion n n² O(1)
Sequential search 1 n O(1)

Example 1.3: Insertion sort on a small array

Problem: Sort 5, 2, 4, 6, 1 by insertion sort and count the comparisons.

Solution:

  1. Insert 2: compare with 5, shift → 2 5 4 6 1 (1 comparison).
  2. Insert 4: compare with 5, then 2 → 2 4 5 6 1 (2 comparisons).
  3. Insert 6: compare with 5 only → 2 4 5 6 1 (1 comparison).
  4. Insert 1: compare with 6, 5, 4, 2 → 1 2 4 5 6 (4 comparisons).

Result: Sorted array 1 2 4 5 6 using 1 + 2 + 1 + 4 = 8 comparisons.

def insertion(a):
    a, comps = a[:], 0
    for i in range(1, len(a)):
        key, j = a[i], i - 1
        while j >= 0:
            comps += 1
            if a[j] <= key:
                break
            a[j + 1] = a[j]
            j -= 1
        a[j + 1] = key
    return a, comps
print(insertion([5, 2, 4, 6, 1]))
print(insertion([1, 2, 3, 4, 5])[1], "comparisons (sorted)")
print(insertion([5, 4, 3, 2, 1])[1], "comparisons (reverse)")

Output

([1, 2, 4, 5, 6], 8)
4 comparisons (sorted)
10 comparisons (reverse)

Common mistakes

  • Saying bubble sort is Θ(n) without the early-exit flag.
  • Counting swaps instead of comparisons when asked for time complexity.
  • Believing selection sort is faster on sorted data; it always does n(n − 1)/2 comparisons.
  • Forgetting that insertion sort's best case occurs on sorted input.

Key takeaways

  • Sequential search: best 1, worst n, average (n + 1)/2 comparisons.
  • Bubble and selection sort do n(n − 1)/2 comparisons; selection makes at most n − 1 swaps.
  • Insertion sort is Θ(n) best, Θ(n²) worst and average.
  • All three sorts are in place with O(1) extra space.

Practice questions

  1. How many comparisons does selection sort make for n = 10?
  2. When is insertion sort fastest?
  3. What is the average number of comparisons in a successful sequential search?

Answers to the practice questions

  1. 10 × 9 / 2 = 45, whatever the input order.
  2. On an already sorted array, taking about n − 1 comparisons.
  3. (n + 1)/2.

Sources

  • Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit I