1. Big-O Comparison: Bubble vs. Selection vs. Insertion vs. std::sort()
Not all O(n²) algorithms behave equally on real inputs. Selection Sort minimizes swaps (at most N − 1 writes), whereas Insertion Sort speeds up to O(n) when an array is already nearly sorted.
| Algorithm | Best Case | Avg / Worst Case | Extra Space | Stable? | Best Use Case |
|---|---|---|---|---|---|
| Bubble Sort (early-exit flag) | O(n) | O(n²) | O(1) | Yes | Small arrays (N ≤ 1,000) or visualizing adjacent swaps |
| Selection Sort | O(n²) | O(n²) | O(1) | No (standard) | Minimizing the total number of memory writes/swaps |
| Insertion Sort | O(n) | O(n²) | O(1) | Yes | Small or nearly sorted inputs, or streaming data |
| Frequency / Counting Sort | O(n + V) | O(n + V) | O(V) | Yes | Small non-negative integers (0 ≤ v[i] ≤ 10⁶) |
| std::sort() (<algorithm>) | O(n log n) | O(n log n) | O(log n) | No (use stable_sort if needed) | Olympiads and production code up to N = 1,000,000+ |
2. Step-by-Step First-Pass Trace on [7, 2, 9, 1, 5]
Multiple-choice exam questions frequently ask how an array looks right after the very first pass (i = 0). Here is how each algorithm transforms [7, 2, 9, 1, 5] after Pass 1:
| Algorithm | Pass 1 Action | Array After Pass 1 | Guaranteed Fixed Element |
|---|---|---|---|
| Bubble Sort | Swaps adjacent pairs (7,2), (9,1), (9,5), bubbling 9 right | [2, 7, 1, 5, 9] | 9 locked at last index |
| Selection Sort | Finds global min (1) and swaps once with v[0]=7 | [1, 2, 9, 7, 5] | 1 locked at index 0 |
| Insertion Sort | Inserts v[1]=2 into sorted prefix [7] | [2, 7, 9, 1, 5] | Prefix [2, 7] sorted |
If a test asks „What is the array after 2 passes of Selection Sort?”, simulate exactly 2 iterations of the outer loop—do not write down the fully sorted array.
3. Clean C++ Implementations: Bubble, Selection & Insertion Sort
Below are standard 0-indexed C++ templates. Notice the early-exit boolean flag in Bubble Sort and value-shifting in Insertion Sort instead of costly three-way swaps:
#include <iostream>
#include <utility>
using namespace std;
void bubbleSort(int v[], int n) {
bool sorted;
int pass = 0;
do {
sorted = true;
for (int i = 0; i < n - 1 - pass; i++) {
if (v[i] > v[i + 1]) {
swap(v[i], v[i + 1]);
sorted = false;
}
}
pass++;
} while (!sorted);
}
void selectionSort(int v[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (v[j] < v[minIdx]) minIdx = j;
}
if (minIdx != i) swap(v[i], v[minIdx]);
}
}
void insertionSort(int v[], int n) {
for (int i = 1; i < n; i++) {
int current = v[i];
int j = i - 1;
while (j >= 0 && v[j] > current) {
v[j + 1] = v[j];
j--;
}
v[j + 1] = current;
}
}
4. Competitive C++ Secret Weapon: std::sort() & Custom Comparators
From <algorithm>, std::sort() uses Introsort (Quicksort + Heapsort + Insertion Sort), guaranteeing O(n log n) even on adversarial inputs. Writing a custom comparator cmp lets you rank structs across multiple tie-breakers cleanly:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct Student {
string name;
int score;
};
bool cmp(const Student& a, const Student& b) {
if (a.score != b.score)
return a.score > b.score; // Higher score first
return a.name < b.name; // Alphabetical tie-breaker
}
int main() {
Student arr[3] = { {"Elena", 95}, {"Alex", 100}, {"Bogdan", 95} };
sort(arr, arr + 3, cmp);
for (int i = 0; i < 3; i++)
cout << arr[i].name << " " << arr[i].score << "\n";
return 0;
}
If your array uses v[1..n], call sort(v + 1, v + n + 1). If it uses v[0..n-1], call sort(v, v + n). Remember the second pointer is exclusive (one past the last element)!