Theory
The principal wants a toppers list
The marks array holds {55, 90, 62, 78, 70}. Two requests land on the program:
1. "Does anyone have exactly 62? Which position?" (search)
2. "Print marks from lowest to highest." (sort)
Humans do both by instinct. A program needs a recipe: precise steps a loop can follow. Today: one search recipe and two sort recipes, together the most guaranteed exam questions in this whole subject.
Theory
Two ways to sort a cricket team by height
Bubble sort is neighbours swapping: compare players 1-2, swap if wrong; then 2-3, swap; down the line. After one full pass, the TALLEST man has bubbled to the end, guaranteed. Repeat for the rest. Selection sort is the coach's method: scan everyone, pick the SHORTEST, put him first; scan the rest, pick the next shortest, put him second. Same result, different temperament: bubble swaps constantly, selection swaps once per round.
Theory
Linear search: just walk
Looking for 62? Walk the coach, berth by berth:
for (i = 0; i < n; i++) {
if (marks[i] == target) {
printf("Found at index %d\n", i);
break; /* why keep looking? */
}
}
Compare, move on, break on success (last lesson earning its keep). Works on ANY order of data, which is its virtue; touching up to all n elements is its cost.
Follow along
Bubble sort, the recipe
- Compare ADJACENT neighbours a[j] and a[j+1] If the left one is bigger, swap them. Only neighbours, never distant pairs.
- Finish one full pass down the array The largest value has now bubbled to the last position, guaranteed and final.
- Repeat the pass on the shrinking unsorted part Each pass may stop one position earlier: j < n-1-i in pass i.
- After n-1 passes, the array is sorted Swapping needs a temp box: t = a; a = b; b = t.
Practical
Bubble sort in C
#include <stdio.h>
int main() {
int marks[5] = {55, 90, 62, 78, 70};
int i, j, temp, n = 5;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (marks[j] > marks[j + 1]) {
temp = marks[j];
marks[j] = marks[j + 1];
marks[j + 1] = temp;
}
}
}
for (i = 0; i < n; i++) printf("%d ", marks[i]);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Trace pass one
Take {55, 90, 62, 78, 70} and run ONE bubble pass on paper: compare positions 0-1, 1-2, 2-3, 3-4, swapping where the left is bigger. What does the array look like after the pass?
Show the answer
55-90: fine. 90-62: swap → {55, 62, 90, 78, 70}. 90-78: swap → {55, 62, 78, 90, 70}. 90-70: swap → {55, 62, 78, 70, 90}.
After one pass: {55, 62, 78, 70, 90}. Not sorted yet, but 90, the largest, has reached its final seat. That one-pass trace, written exactly like this, is a standing exam question.
Quiz
After the FIRST complete pass of bubble sort on any array, what is guaranteed?
- The largest element is in the last position
- The whole array is fully sorted
- The smallest element is in the first position
- Nothing is guaranteed until all passes finish
Show the answer
The largest element is in the last position
Each comparison pushes the bigger neighbour rightward, so the pass sweeps the maximum all the way to the end, its final home. The smallest reaching the front is SELECTION sort's pass-one promise (when selecting minimums), the two guarantees are a classic exam confusion pair.
Quiz
To swap marks[j] and marks[j+1], a student writes: marks[j] = marks[j+1]; marks[j+1] = marks[j]; What actually happens?
- Both boxes end up holding the same value; the original marks[j] is lost
- The values swap correctly
- A compile error: swapping needs a function
- The array becomes empty
Show the answer
Both boxes end up holding the same value; the original marks[j] is lost
The first assignment OVERWRITES marks[j], destroying its old value before anyone saved it; the second then copies that same value back. Result: duplicates, no swap. The fix is the temp variable: temp = marks[j]; marks[j] = marks[j+1]; marks[j+1] = temp. The lost-value swap is among the most beloved spot-the-bug questions.
Watch out
Where marks leak
Swapping without temp (the quiz above). Wrong inner bound: j must stop at n-1-i; run j to n-1 and marks[j+1] steps out of bounds on the last compare. Mixing the two sorts in comparisons: bubble swaps adjacent neighbours many times per pass; selection scans for the minimum and makes AT MOST ONE swap per pass. State that difference and the compare-the-sorts question is yours.
Theory
You just met algorithms
These recipes have a grander name, algorithms, and BCA304 devotes a whole subject to them (where you will also learn WHY bubble sort is slow for big data, and meet binary search, which finds items in a sorted array absurdly fast). For now: the marks program can find any student and print a merit list. Next: the grid, students × subjects, two-dimensional arrays.
Summary
Key takeaways
- Linear search: walk 0 to n-1, compare, break when found; works unsorted.
- Bubble sort: swap out-of-order ADJACENT pairs; each pass bubbles the max to the end.
- Inner bubble loop: j < n-1-i; n-1 passes sort n elements.
- Swap needs temp: t = a; a = b; b = t (without it, a value is lost).
- Selection sort: find the minimum, one swap per pass into the front.
- Memory hook: bubbles rise, the biggest reaches the top (end) first.