Selection Sort and Bubble Sort Pass by Pass (ICSE Class 10 Java)
Updated 17 October 2026 · Fact-checked
Selection sort finds the smallest element of the unsorted part and swaps it into the next position, repeating n - 1 times. Bubble sort compares neighbouring elements and swaps them if they are in the wrong order, so the largest element "bubbles" to the end in each pass. Both need two nested loops and a temporary variable for swapping, and both make n(n - 1)/2 comparisons when written in the simple form.
Understand Selection Sort and Bubble Sort Pass by Pass
Sorting arranges array elements in ascending (small to large) or descending order. The syllabus asks for selection sort and bubble sort on single-dimensional arrays.
Swapping needs a temporary variable
int t = a[i];
a[i] = a[j];
a[j] = t;Writing a[i] = a[j]; a[j] = a[i]; loses a value and leaves both equal.
Selection sort (ascending). In pass i (i = 0 to n - 2), find the index m of the smallest element from position i to the end, then swap a[m] with a[i].
for (int i = 0; i < n - 1; i++) {
int m = i;
for (int j = i + 1; j < n; j++)
if (a[j] < a[m]) m = j;
int t = a[m]; a[m] = a[i]; a[i] = t;
}After pass i the first i + 1 positions hold their final values.
Bubble sort (ascending). In pass i compare a[j] and a[j + 1] for j = 0 to n - 2 - i and swap if a[j] > a[j + 1].
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;
}After pass i the largest i + 1 elements are at the end.
Descending order. Change the comparison: a[j] < a[m] becomes a[j] > a[m] in selection sort (find the largest), and a[j] > a[j + 1] becomes a[j] < a[j + 1] in bubble sort.
Counting. Both sorts take n - 1 passes. The comparisons are (n - 1) + (n - 2) + ... + 1 = n(n - 1)/2: for n = 5 that is 10, for n = 6 it is 15. The number of swaps in bubble sort equals the number of pairs that are out of order; selection sort makes at most one swap per pass.
Selection Sort and Bubble Sort Pass by Pass: key facts
- Swap with a temporary variable
- t = a[i]
- a[i] = a[j]
- a[j] = t
- Three statements, in this order.
- Passes and comparisons
Passes n - 1 Comparisons n(n - 1)/2 n = 5 10 n = 6 15 - For the simple version without early exit.
- Selection sort rule
- Pass i: find the smallest from i to n - 1 | Swap it with a[i]
- At most one swap per pass.
- Bubble sort rule
- Pass i: compare a[j] and a[j+1] for j = 0 to n - 2 - i | Swap if a[j] is greater than a[j+1]
- For descending use less than.
How to answer Selection Sort and Bubble Sort Pass by Pass questions
Use this routine for tracing a sort.
- 1Copy the array in a row and number the indices.
- 2For selection sort: for each pass find the smallest value in the unsorted part, note its index and swap; write the array after each pass.
- 3For bubble sort: for each pass go through neighbouring pairs from the left, swap when out of order, and write the array after the pass.
- 4Shorten the inner loop by one for each completed pass (the end is already in place).
- 5Count the swaps and the comparisons if asked.
Selection Sort and Bubble Sort Pass by Pass 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.
Selection sort with a line of students
Suppose a teacher lines students up by height, and in every round she picks the shortest student from those still unplaced and sends him or her to the next spot at the front.
Which sorting technique is this, and what does round 1 do for heights 140, 128, 135 cm?
Try it first, then show the solutionSolution
- Choosing the smallest and moving it to the front is selection sort.
- Round 1 picks 128 and swaps it with 140: the line becomes 128, 140, 135.
Answer: Selection sort; after round 1: 128, 140, 135.
Memory hook: Select the smallest, place it.
Tricks to remember Selection Sort and Bubble Sort Pass by Pass
Short memory aids. Say each one aloud once, then use it on a question the same day.
Select = search then one swap
Selection sort searches the rest of the array and swaps once per pass.
Use it when: Describing selection sort.
Bubble = neighbours
Bubble sort only compares neighbours and bubbles the biggest to the end.
Use it when: Describing bubble sort.
n - 1 passes
The last element falls in place by itself, so n - 1 passes are enough.
Use it when: Loop limits.
Selection Sort and Bubble Sort Pass by Pass: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Principle of each sort · 2 marks · Short answerBoard level
State in one line the working of (i) selection sort and (ii) bubble sort for ascending order.
Try it first, then show the solutionSolution
- (i) In every pass the smallest element of the unsorted part is found and swapped into the next position. (1 mark)
- (ii) In every pass adjacent elements are compared and swapped if the left one is greater, so the largest moves to the end. (1 mark)
Answer: Selection: pick the smallest and place it. Bubble: swap neighbours that are out of order.
Example 2: Two passes of selection sort · 3 marks · Short answerHarder board level
Write the output:
int[] a = {64, 25, 12, 22, 11};
int n = a.length;
for (int i = 0; i < 2; i++) {
int m = i;
for (int j = i + 1; j < n; j++)
if (a[j] < a[m]) m = j;
int t = a[m]; a[m] = a[i]; a[i] = t;
}
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");Try it first, then show the solutionSolution
- Pass 0: the smallest is 11 at index 4; swap with index 0: 11 25 12 22 64. (1 mark)
- Pass 1: the smallest of 25, 12, 22, 64 is 12 at index 2; swap with index 1: 11 12 25 22 64. (1 mark)
- Only two passes were run, so the array is printed as 11 12 25 22 64 (each followed by a space). (1 mark)
Answer: 11 12 25 22 64
Example 3: First pass of bubble sort · 3 marks · Short answerHarder board level
Write the output:
int[] a = {5, 1, 4, 2, 8};
int n = a.length;
for (int j = 0; j < n - 1; j++)
if (a[j] > a[j + 1]) {
int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
}
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");Try it first, then show the solutionSolution
- j = 0: 5 > 1, swap: 1 5 4 2 8. j = 1: 5 > 4, swap: 1 4 5 2 8. (1 mark)
- j = 2: 5 > 2, swap: 1 4 2 5 8. j = 3: 5 > 8 is false, no swap. (1 mark)
- Output: 1 4 2 5 8; the largest value 8 is at the end after the first pass. (1 mark)
Answer: 1 4 2 5 8
Example 4: Case study: bubble sort in descending order · 4 marks · Case-basedHOTS / competency
A program sorts {5, 1, 4, 2, 8} in descending order:
int[] a = {5, 1, 4, 2, 8};
int n = a.length, swaps = 0;
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;
swaps++;
}
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
System.out.println(swaps);(i) Show the array after each pass. (2)
(ii) State the output. (1)
(iii) How many comparisons are made in all? (1)
Try it first, then show the solutionSolution
- (i) Pass 1: 5 4 2 8 1 (swaps at 1-4, 1-2, 1-8). Pass 2: 5 4 8 2 1. Pass 3: 5 8 4 2 1. Pass 4: 8 5 4 2 1.
- (ii) Swaps: 3 + 1 + 1 + 1 = 6. Output: 8 5 4 2 1 6
- (iii) The inner loop runs 4, 3, 2 and 1 times: 4 + 3 + 2 + 1 = 10 comparisons.
Answer: (ii) 8 5 4 2 1 6 (iii) 10 comparisons
Example 5: Counting for n elements · 3 marks · Short answerHarder board level
For an array of 6 elements, state the number of passes and the number of comparisons made by the simple selection sort, and the maximum number of swaps it makes.
Try it first, then show the solutionSolution
- Passes: n - 1 = 5. (1 mark)
- Comparisons: 5 + 4 + 3 + 2 + 1 = 15. (1 mark)
- At most one swap in each pass: at most 5 swaps. (1 mark)
Answer: 5 passes, 15 comparisons, at most 5 swaps
Where students lose marks in Selection Sort and Bubble Sort Pass by Pass
The slips examiners see most often, and what to write instead.
Mistake: Swapping without a temporary variable
a[i] = a[j]; a[j] = a[i]; makes both elements equal to the old a[j].
Wrong working
a[i] = a[j]; a[j] = a[i];
Right working
int t = a[i]; a[i] = a[j]; a[j] = t;
Do this: Use three statements and t.
Usually costs: 1 to 2 marks
Mistake: Running the bubble inner loop up to n - 1
a[j + 1] would be a[n], an index outside the array: ArrayIndexOutOfBoundsException.
Wrong working
for (j = 0; j < n; j++) if (a[j] > a[j+1])
Right working
for (j = 0; j < n - 1 - i; j++)
Do this: The last j must be n - 2.
Usually costs: 1 mark
Mistake: Swapping inside the inner loop of selection sort every time a smaller value is found
The selection sort swaps once per pass, after finding the smallest index.
Wrong working
if (a[j] < a[i]) swap in the j loop
Right working
Record m = j in the loop; swap after the loop
Do this: Find first, swap later.
Usually costs: 1 mark
Exam tips
- Show the array after each pass in a row; it gives marks even if a later step slips.
- Write the swap with the temporary variable every time you write code.
- For descending order only the comparison sign changes; say so.
- Remember: the syllabus requires sorting on single-dimensional arrays only.
Practice questions on Selection Sort and Bubble Sort Pass by Pass
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
In bubble sort for ascending order, two adjacent elements a[j] and a[j+1] are interchanged when:
(a) a[j] < a[j+1] (b) a[j] > a[j+1] (c) a[j] == a[j+1] (d) j is evenHint
The bigger one must move right.
Answer
(b) a[j] > a[j+1]
Q2 · 1 mark · Very short answerBoard level
After the first pass of selection sort in ascending order, which value is at index 0?
Hint
Smallest selected.
Answer
The smallest element of the array.
Q3 · 2 marks · Short answerHarder board level
Write the array after the first pass of selection sort (ascending) on {7, 3, 9, 1}.
Hint
Find the smallest and swap with index 0.
Answer
{1, 3, 9, 7} (1 at index 3 is swapped with 7 at index 0)
Q4 · 3 marks · Short answerHarder board level
Trace bubble sort (ascending) on {3, 2, 1}. Show the array after each pass and state the number of swaps made.
Hint
Pass 1 has two comparisons, pass 2 has one.
Answer
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. Total swaps: 3.
Q5 · 1 mark · Assertion-reasonHOTS / competency
Assertion (A): After n - 1 passes of selection sort on an array of n elements, the array is sorted.
Reason (R): Each pass places the next smallest element at its correct position.
(a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.Hint
What about the last element?
Answer
(a) Both are true and R explains A: when n - 1 elements are in place the last one is too.
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.How many passes does selection sort need for n elements?Show answer
n - 1.
2.Which element is in its final place after bubble sort pass 1 (ascending)?Show answer
The largest.
3.What is the variable t used for in a swap?Show answer
To hold one value temporarily.
4.How many comparisons for n = 5 in the simple version?Show answer
10
5.Which sort swaps adjacent elements?Show answer
Bubble sort.
6.What changes to sort in descending order?Show answer
The comparison operator.
Selection Sort and Bubble Sort Pass by Pass: frequently asked questions
Which is better, selection sort or bubble sort?
Both make the same number of comparisons in the simple form; selection sort makes fewer swaps. For the exam you must know to write and trace both.
Why does the bubble inner loop shrink?
After each pass the largest remaining element is at the end, so there is no need to compare it again.
Can the same code sort characters?
Yes, with a char array, since characters compare by their codes.