Sorting Programs in C – Bubble, Selection, Insertion
In this lesson: Bubble, selection and insertion sort programs in C with pass-by-pass dry run, complexity table and output. Hindi & English.
What is sorting?
Sorting means putting an array in order — usually smallest to largest. It is one of the most common tasks in programming, and writing a sort by hand teaches loops, comparisons and swapping all at once. Here are the three classic beginner algorithms, each doing the same job a different way.
All three sort the array in place and share the same helper — a swap of two elements.
Bubble sort
Compare each neighbour pair and swap if out of order. The biggest value "bubbles" to the end each pass.
void bubbleSort(int a[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1 - i; j++)
if (a[j] > a[j + 1]) {
int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
}
}
| Compare | Pair | Swap? | Array ab |
|---|---|---|---|
| a[0] & a[1] | 5, 2 | haan (5>2) | 2, 5, 4, 1 |
| a[1] & a[2] | 5, 4 | haan (5>4) | 2, 4, 5, 1 |
| a[2] & a[3] | 5, 1 | haan (5>1) | 2, 4, 1, 5 |
| Pehla pass poora — sabse bada (5) end me pahunch gaya. Aise hi agle pass chalte hain jab tak array sort na ho: 1, 2, 4, 5 | |||
Bubble sort paas-paas wale do numbers compare karta hai aur bade ko aage bhejta hai. Har pass me sabse bada number end me "bubble" hokar chala jaata hai. Kai pass ke baad poora array sort ho jaata hai.
Dry run on {5, 2, 4} — first pass: compare 5&2 → swap → {2,5,4}; compare 5&4 → swap → {2,4,5}. Sorted.
Selection sort
Find the smallest element in the unsorted part and put it in the next position. Repeat.
void selectionSort(int a[], int n) {
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++)
if (a[j] < a[min]) min = j; // find smallest
int t = a[i]; a[i] = a[min]; a[min] = t; // place it
}
}Each round selects the minimum of what is left and swaps it into place — so it makes at most one swap per round.
Insertion sort
Take each element and slide it back into its correct spot among the already-sorted elements, like sorting playing cards in your hand.
void insertionSort(int a[], int n) {
for (int i = 1; i < n; i++) {
int key = a[i], j = i - 1;
while (j >= 0 && a[j] > key) { // shift bigger ones right
a[j + 1] = a[j];
j--;
}
a[j + 1] = key; // drop key into place
}
}If the array is already almost in order, insertion sort barely moves anything and finishes very fast.
Comparing the three
| Algorithm | Idea | Worst case | Best for |
|---|---|---|---|
| Bubble | Swap neighbours repeatedly | O(n²) | Learning the concept |
| Selection | Pick the smallest each round | O(n²) | Fewest swaps |
| Insertion | Insert into sorted part | O(n²) | Nearly-sorted data |
All three are O(n²), fine for small arrays. For large data, faster O(n log n) methods like quicksort are used. To find an item in a sorted array, pair these with binary search.
Common mistakes
- Looping too far and reading past the array (watch
n - 1bounds). - Swapping without a temporary variable, losing a value.
- In insertion sort, forgetting to place
keyafter thewhileloop. - Comparing with
<vs>the wrong way, sorting in the wrong direction.
Add a line inside each algorithm to print the array after every pass, and sort {5, 2, 4, 1, 3} with all three. Watching each pass makes the differences between them clear.
Summary
- Sorting arranges an array in order; C has no automatic sort for you.
- Bubble sort swaps neighbours until no swaps are left.
- Selection sort repeatedly places the smallest remaining element.
- Insertion sort slides each element into the sorted part; great when nearly sorted.
- All three are O(n²) — good for small arrays; use O(n log n) methods for large data.
Frequently Asked Questions
What is sorting in C?
qsort — to rearrange the array in place.