Sorting arranges data in order. Sorted data is easier to search, to merge and to read. The three simple sorts below each take O(n²) time but are easy to understand.
Bubble Sort
Bubble sort compares each pair of adjacent elements and swaps them if they are in the wrong order. After the first pass, the largest element has "bubbled" to the end. After pass i, the last i elements are in place. For n elements, at most n − 1 passes are needed. If a pass makes no swap, the array is already sorted, and the sort can stop early, so its best case is O(n). Its worst and average cases are O(n²).
Definition: Sorting. The process of arranging the elements of a collection in a definite order, ascending or descending, by the value of a key.
Selection Sort
Selection sort finds the smallest element in the unsorted part and swaps it with the first element of that part. After pass i, the first i elements are sorted. It always makes about n²/2 comparisons, so it is O(n²) in every case, but it makes at most n − 1 swaps, which helps when writing to memory is costly.
Insertion Sort
Insertion sort builds the sorted list one element at a time, like arranging playing cards in the hand. Each new element is taken from the unsorted part and inserted into its correct place in the sorted part, shifting the larger elements right. Its worst case is O(n²), but on data that is nearly sorted it is close to O(n), and it is efficient for small arrays. It is stable, keeping equal elements in their original order, and so is bubble sort, while the simple selection sort is not.
Table 1.5: Comparison of simple sorts
| Sort | Best | Worst | Swaps | Stable |
|---|---|---|---|---|
| Bubble | O(n) | O(n²) | Many | Yes |
| Selection | O(n²) | O(n²) | At most n − 1 | No |
| Insertion | O(n) | O(n²) | Shifts | Yes |
Example 1.5: Passes of bubble sort
Problem: Show the passes of bubble sort on {5, 1, 4, 2, 8}.
Solution:
- Pass 1: (5,1) swap → 1 5 4 2 8; (5,4) swap → 1 4 5 2 8; (5,2) swap → 1 4 2 5 8; (5,8) no swap.
- Pass 2: 1 4 2 5 8 → (4,2) swap → 1 2 4 5 8.
- Pass 3: no swaps, so the array is sorted.
Result: The sorted array is {1, 2, 4, 5, 8}, after three passes.
#include <stdio.h>
void show(int a[], int n) {
for (int i = 0; i < n; i++) printf("%d ", a[i]);
printf("\n");
}
int main(void) {
int a[] = {5, 1, 4, 2, 8}, b[] = {29, 10, 14, 37, 13}, n = 5;
for (int p = 0; p < n - 1; p++)
for (int j = 0; j < n - 1 - p; j++)
if (a[j] > a[j + 1]) {
int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
}
printf("bubble: "); show(a, n);
for (int i = 1; i < n; i++) {
int key = b[i], j = i - 1;
while (j >= 0 && b[j] > key) { b[j + 1] = b[j]; j--; }
b[j + 1] = key;
}
printf("insertion: "); show(b, n);
return 0;
}
Output
bubble: 1 2 4 5 8
insertion: 10 13 14 29 37
Common mistakes
- Running the inner loop of bubble sort to n instead of n − 1 − pass, which reads past the end.
- Swapping inside the inner loop of selection sort instead of after finding the minimum.
- Forgetting that all three simple sorts are O(n²) in the worst case.
Key takeaways
- Bubble sort swaps adjacent pairs, and the largest bubbles to the end.
- Selection sort selects the minimum and swaps it to the front.
- Insertion sort inserts each element into the sorted part.
- All three are O(n²) in the worst case; insertion sort is fast on nearly sorted data.
Practice questions
- How does selection sort work?
- What is the best case of bubble sort with an early stop?
- What is a stable sort?
Answers to the practice questions
- It repeatedly finds the smallest element of the unsorted part and swaps it to the front of that part.
- O(n), when the array is already sorted.
- One that keeps equal elements in their original relative order.
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit I
