Sub topic 8
Insertion, Selection, and Bubble Sort
Assume you know arrays, indexing, loops, and basic asymptotic notation. We will sort an array into nondecreasing order, so smaller values appear first. Each algorithm uses comparisons, but it places elements in a different way.
Insertion Sort
Insertion sort builds a sorted prefix one element at a time. At step i, the elements before i are already sorted. The algorithm removes a[i], shifts larger elements one position right, and inserts the removed value into its correct position.
def insertion_sort(a):
for i in range(1, len(a)):
key = a[i]
j = i - 1
while j >= 0 and a[j] > key:
a[j + 1] = a[j]
j -= 1
a[j + 1] = keyIf the array is already sorted, the inner loop does no shifting. The best-case time is therefore Θ(n). In reverse order, each new element moves across the whole sorted prefix, giving Θ(n²). The average-case time is also Θ(n²). Insertion sort is in-place and stable because equal elements are not moved past one another.
Selection Sort
Selection sort repeatedly chooses the smallest element from the unsorted suffix and exchanges it with the first element of that suffix.
def selection_sort(a):
for i in range(len(a) - 1):
smallest = i
for j in range(i + 1, len(a)):
if a[j] < a[smallest]:
smallest = j
a[i], a[smallest] = a[smallest], a[i]The inner loop always scans the remaining suffix, regardless of its order. The number of comparisons is
\[ (n-1) + (n-2) + \cdots + 1 = \Theta(n^2). \]
Thus, best, average, and worst cases are all Θ(n²). Selection sort uses Θ(1) extra space and performs at most n - 1 swaps. The usual version is not stable because a swap can move an equal element past another equal element.
Bubble Sort
Bubble sort repeatedly compares adjacent elements and swaps them when they are out of order. After one full pass, the largest remaining element reaches the end of the unsorted region.
def bubble_sort(a):
for end in range(len(a) - 1, 0, -1):
swapped = False
for j in range(end):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
swapped = True
if not swapped:
breakThe swapped flag gives an early exit. With this improvement, an already sorted array takes Θ(n) time. The average and worst cases take Θ(n²). Bubble sort is in-place and stable when it swaps only on > rather than >=.
Comparison
| Algorithm | Best time | Average time | Worst time | Stable | In-place |
|---|---|---|---|---|---|
| Insertion | Θ(n) | Θ(n²) | Θ(n²) | Yes | Yes |
| Selection | Θ(n²) | Θ(n²) | Θ(n²) | Usually no | Yes |
| Bubble, optimized | Θ(n) | Θ(n²) | Θ(n²) | Yes | Yes |
Insertion sort is often the best choice for small or nearly sorted inputs. Selection sort minimizes swaps. Bubble sort is mainly useful for teaching or for recognizing adjacent inversions in exam questions.
Exercise
Which algorithm has Θ(n) best-case time when implemented as shown, and what causes this best case?
Show answer
Insertion sort and optimized bubble sort. Insertion sort finds every item already in place, while bubble sort completes a pass without any swap and exits.