Bubble, Selection and Insertion Sort and Binary Search (ISC Class 12 Computer Science)
Updated 17 October 2026 · Draft, not yet fact-checked
Bubble sort swaps adjacent out-of-order pairs, so the largest value moves to the end on every pass. Selection sort picks the smallest of the unsorted part and swaps it to the front. Insertion sort inserts each new element into the sorted part on its left by shifting larger ones right. Binary search needs a sorted array and halves the range each step.
Understand Sorting by Bubble, Selection and Insertion and Binary Search
Arrays. An array stores elements of the same type under one name; the index starts at 0 and the element access a[i] takes constant time whatever the size. a.length is the number of elements.
Bubble sort (ascending). In pass k, compare neighbours a[j] and a[j+1] for j from 0 to n-k-1 and swap if a[j] > a[j+1]. After pass 1 the largest is in the last place. n - 1 passes are enough. Code:
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;
}A flag that records whether any swap occurred lets the loop stop early on sorted data.
Selection sort. For each position i from 0 to n-2, find the index of the smallest element in a[i] to a[n-1] and swap it with a[i]. At most n - 1 swaps; always about n(n-1)/2 comparisons.
Insertion sort. For i from 1 to n-1, hold key = a[i], shift every larger element of a[0..i-1] one place right, and drop the key in the gap. Fast on nearly sorted data (about n comparisons if already sorted).
for (int i = 1; i < n; i++) {
int key = a[i], k = i - 1;
while (k >= 0 && a[k] > key) { a[k + 1] = a[k]; k--; }
a[k + 1] = key;
}Linear search. Compare the key with each element in turn; works on any array; at most n comparisons.
Binary search. The array must be sorted. Keep lo and hi; mid = (lo + hi) / 2; if a[mid] equals the key, found; if smaller, lo = mid + 1; otherwise hi = mid - 1; stop when lo > hi (not found). Each step halves the range, so about log2(n) steps are enough.
Two-dimensional arrays and address calculation, and strings, are separate topics.
Sorting by Bubble, Selection and Insertion and Binary Search: key facts
- Bubble sort counts
- Passes: n - 1
- Comparisons without a flag: n(n-1)/2
- Swaps = number of inversions
- Largest goes last each pass
- For n = 5, comparisons are 4 + 3 + 2 + 1 = 10.
- Selection and insertion sort
- Selection: n - 1 passes, at most n - 1 swaps
- Insertion: n - 1 insertions, shifts instead of swaps
- Insertion is fastest on nearly sorted data
- Selection does the same comparisons on any data.
- Binary search step
- mid = (lo + hi) / 2
- a[mid] < key: lo = mid + 1
- a[mid] > key: hi = mid - 1
- Stop when lo > hi
- Needs a sorted array.
- Searching effort
- Linear search: up to n comparisons
- Binary search: about log2 n comparisons
- 1000 sorted items need at most 10 steps
- Binary search halves the range each step.
How to answer Sorting by Bubble, Selection and Insertion and Binary Search questions
Use this routine to trace a sort or search.
- 1Write the array and draw the sorted and unsorted regions.
- 2Bubble sort: write the array after every pass and mark each swap.
- 3Selection sort: find the minimum of the unsorted part, note its index, then swap it to the front.
- 4Insertion sort: take the key, shift larger elements right one by one, and place the key.
- 5Binary search: write lo, hi and mid for every step until found or lo > hi.
- 6Count the swaps, shifts or comparisons the question asks for.
Sorting by Bubble, Selection and Insertion and Binary Search in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Arranging exam papers
Suppose a teacher picks up papers one at a time and slides each into its place among the papers already arranged by roll number.
Which sorting technique is this, and is it fast when the papers are almost arranged?
Try it first, then show the solutionSolution
- Taking each item and inserting it into the sorted part is insertion sort.
- On almost-sorted data each insertion needs few shifts, so it is fast.
Answer: Insertion sort; yes, it is fast on almost sorted data.
Memory hook: Insert like cards in your hand.
Looking up a name in a sorted list
Suppose a sorted list has 1000 names and the program opens the middle name, then the middle of the half that can contain the target, and so on.
What is the largest number of names it has to open?
Try it first, then show the solutionSolution
- Each step halves the range: 1000, 500, 250, 125, 62, 31, 15, 7, 3, 1.
- That is 10 steps because 2 to the power 10 is 1024, which is at least 1000.
Answer: At most 10.
Memory hook: Halve until one is left.
Tricks to remember Sorting by Bubble, Selection and Insertion and Binary Search
Short memory aids. Say each one aloud once, then use it on a question the same day.
Bubble: biggest floats to the end
After pass 1 the largest is at the last place; after pass 2 the second largest.
Use it when: Bubble sort tracing.
Selection: select, then swap
Find the minimum of the rest, then do one swap.
Use it when: Selection sort tracing.
Insertion: cards in hand
Pick up a card, shift bigger ones right, slot it in.
Use it when: Insertion sort tracing.
Binary needs order
Sorted first, then halve: lo, mid, hi.
Use it when: Binary search conditions.
Sorting by Bubble, Selection and Insertion and Binary Search: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Name the sort · 1 mark · MCQBoard level
The sorting technique that repeatedly selects the smallest element of the unsorted part and swaps it into place is
(a) bubble sort (b) insertion sort (c) selection sort (d) binary sort
Try it first, then show the solutionSolution
- Selecting the minimum and swapping it to the front is selection sort.
Answer: (c) selection sort
Example 2: Bubble sort trace · 4 marks · Short answerHarder board level
Sort 5, 1, 4, 2, 8 in ascending order by bubble sort. Show the array after each pass and count the swaps.
Try it first, then show the solutionSolution
- Pass 1: (5,1) swap gives 1 5 4 2 8; (5,4) swap gives 1 4 5 2 8; (5,2) swap gives 1 4 2 5 8; (5,8) no swap. Array: 1 4 2 5 8, 3 swaps. (1.5 marks)
- Pass 2: (1,4) no swap; (4,2) swap gives 1 2 4 5 8; (4,5) no swap. Array: 1 2 4 5 8, 1 swap. (1.5 marks)
- Pass 3 finds no swap, so the array is sorted. Total swaps 3 + 1 = 4. (1 mark)
Answer: Pass 1: 1 4 2 5 8; pass 2: 1 2 4 5 8; total 4 swaps
Example 3: Selection sort trace · 4 marks · Short answerHarder board level
Sort 64, 25, 12, 22, 11 in ascending order by selection sort. Show the array after each pass.
Try it first, then show the solutionSolution
- Pass 1: the minimum 11 is at index 4; swap with index 0: 11 25 12 22 64. (1 mark)
- Pass 2: minimum of 25 12 22 64 is 12 at index 2; swap with index 1: 11 12 25 22 64. (1 mark)
- Pass 3: minimum of 25 22 64 is 22 at index 3; swap with index 2: 11 12 22 25 64. (1 mark)
- Pass 4: minimum of 25 64 is 25, already in place: 11 12 22 25 64. Three real swaps. (1 mark)
Answer: 11 12 22 25 64 after four passes (3 swaps)
Example 4: Insertion sort trace · 3 marks · Short answerHarder board level
Sort 7, 3, 5, 2 by insertion sort. Show the array after each insertion and count the shifts.
Try it first, then show the solutionSolution
- Key 3: 7 shifts right: 3 7 5 2 (1 shift). (1 mark)
- Key 5: 7 shifts right, 3 stays: 3 5 7 2 (1 shift). (1 mark)
- Key 2: 7, 5 and 3 all shift: 2 3 5 7 (3 shifts). Total 5 shifts. (1 mark)
Answer: 2 3 5 7 with 5 shifts
Example 5: Case: binary search in a sorted list · 4 marks · Case-basedHOTS / competency
Suppose a sorted array is a = {3, 8, 12, 19, 25, 31, 40} and this method is used:
static int bsearch(int[] a, int key) {
int lo = 0, hi = a.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (a[mid] == key) return mid;
else if (a[mid] < key) lo = mid + 1;
else hi = mid - 1;
}
return -1;
}(i) Find bsearch(a, 31) with the values of mid. (2 marks)
(ii) Find bsearch(a, 10) with the values of mid. (1 mark)
(iii) At most how many steps does binary search take for 1000 sorted items? (1 mark)
Try it first, then show the solutionSolution
- (i) lo = 0, hi = 6: mid = 3, a[3] = 19 < 31, so lo = 4. Then mid = (4 + 6) / 2 = 5, a[5] = 31 matches: returns 5.
- (ii) mid = 3 (19 > 10), hi = 2; mid = 1 (8 < 10), lo = 2; mid = 2 (12 > 10), hi = 1; now lo > hi, so it returns -1.
- (iii) 10 steps, since 2 to the power 10 = 1024 >= 1000.
Answer: (i) 5 (ii) -1 (mids 3, 1, 2) (iii) 10
Where students lose marks in Sorting by Bubble, Selection and Insertion and Binary Search
The slips examiners see most often, and what to write instead.
Mistake: Inner loop of bubble sort runs to n instead of n - 1 - i
With j < n, the last pass of the loop has j = n - 1, so a[j + 1] is a[n], which is out of bounds and throws ArrayIndexOutOfBoundsException. Not reducing the limit by i is a separate, smaller fault: it only adds redundant comparisons with elements already in place, and the array still gets sorted.
Wrong working
for (j = 0; j < n; j++) compares a[j] with a[j + 1]
Right working
for (j = 0; j < n - 1 - i; j++)
Do this: Use n - 1 - i as the limit.
Usually costs: 1 mark
Mistake: Binary search on an unsorted array
Halving relies on order; an unsorted array gives wrong answers.
Wrong working
Applying mid comparison on 8 3 5 1
Right working
Sort first, then search
Do this: State the pre-condition.
Usually costs: 1 mark
Mistake: Wrong mid update
lo = mid or hi = mid can loop for ever.
Wrong working
else if (a[mid] < key) lo = mid;
Right working
else if (a[mid] < key) lo = mid + 1;
Do this: Move past mid on each side.
Usually costs: 1 mark
Exam tips
- Show the array after every pass in a sorting trace; the marking is per pass.
- State the number of swaps or comparisons only after listing them.
- Write the pre-condition (sorted array) for binary search.
- Use the temporary variable t for every swap.
- Name the sorting technique and its idea in one line before the code.
Practice questions on Sorting by Bubble, Selection and Insertion and Binary Search
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
Binary search can be applied only when the array is
(a) of integers (b) sorted (c) of even length (d) two-dimensionalHint
The halving decision depends on order.
Answer
(b) sorted
Q2 · 2 marks · Short answerBoard level
How many passes does the basic bubble sort need for an array of 6 elements, and which element is in its final position after pass 1?
Hint
n - 1 passes.
Answer
Five passes (n - 1). After pass 1 the largest element is in the last position.
Q3 · 4 marks · Short answerHarder board level
Write a complete bubble sort for an int array a of length n in ascending order and trace it for 3, 2, 1.
Hint
Inner loop limit n - 1 - i.
Answer
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; }Trace for 3 2 1: pass 1: (3,2) swap gives 2 3 1; (3,1) swap gives 2 1 3. Pass 2: (2,1) swap gives 1 2 3. Three swaps; sorted: 1 2 3.
Q4 · 3 marks · Short answerHarder board level
Sort 4, 9, 1, 7 in descending order by selection sort, showing the array after each pass.
Hint
Select the largest of the unsorted part each time.
Answer
Pass 1: largest 9 (index 1) swaps with index 0: 9 4 1 7. Pass 2: largest of 4 1 7 is 7 (index 3) swaps with index 1: 9 7 1 4. Pass 3: largest of 1 4 is 4 swaps with index 2: 9 7 4 1. Sorted: 9 7 4 1.
Q5 · 4 marks · Case-basedHOTS / competency
For the already sorted array 1, 2, 3, 4, 5, count the comparisons made by (a) basic bubble sort with no flag, (b) bubble sort with a swap flag, (c) selection sort and (d) insertion sort. Which technique suits nearly sorted data best?
Hint
Count the comparisons per pass.
Answer
(a) 4 + 3 + 2 + 1 = 10 comparisons. (b) The first pass makes 4 comparisons with no swap, so it stops: 4. (c) Selection sort always finds the minimum over the rest: 4 + 3 + 2 + 1 = 10 comparisons and no real swaps. (d) Insertion sort compares each key once with its left neighbour: 4 comparisons. Insertion sort (or flagged bubble sort) suits nearly sorted data.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.Which element is in place after pass 1 of bubble sort?Show answer
The largest.
2.Which sort shifts elements instead of swapping them?Show answer
Insertion sort.
3.What is the limit of the inner loop in bubble sort?Show answer
n - 1 - i.
4.How many comparisons does selection sort make for n = 5?Show answer
10.
5.What is mid in binary search?Show answer
(lo + hi) / 2.
6.What does binary search return when the key is absent?Show answer
-1 in the usual program (lo exceeds hi).
Sorting by Bubble, Selection and Insertion and Binary Search: frequently asked questions
What is the difference between bubble sort and selection sort?
Bubble sort swaps neighbouring elements many times as the largest moves to the end; selection sort finds the smallest of the unsorted part and does one swap per pass.
Why does binary search need a sorted array?
It decides which half to discard by comparing the key with the middle element, which is valid only if the elements are in order.
Which is faster, linear or binary search?
Binary search needs about log2 n comparisons against up to n for linear search, but it needs a sorted array.
Which sorting techniques are in the ISC Class 12 syllabus?
The syllabus lists bubble, selection and insertion sort.