KwickAcademy Java · 7 min · free
Sorting Arrays: Bubble, Selection and Insertion Sort
Swapping with a temporary variable, then bubble, selection and insertion sort traced pass by pass, with code and a comparison.
Follows the syllabus of: CBSE Class 11 Information Technology (802), ICSE Class 10 Computer Applications, ISC Class 11 Computer Science (868), ISC Class 12 Computer Science (868)
On screen in this lesson
What is sorting?
| Sorting arranges values in order |
| Ascending: smallest to largest |
| Descending: largest to smallest |
| A pass is one full round through the array |
Bubble sort: the idea
| Compare neighbours: a[j] and a[j + 1] |
| Swap them if the left one is bigger |
| After each pass, the largest value reaches the end |
| n values need at most n - 1 passes |
Bubble sort pass by pass
| Pass | Array after | Fixed at end |
|---|---|---|
| Start | 5 1 4 2 | none |
| Pass 1 | 1 4 2 5 | 5 |
| Pass 2 | 1 2 4 5 | 4 and 5 |
| Pass 3 | 1 2 4 5 | all sorted |
Pause and predict
| 5 values: how many comparisons in pass 1? |
| Pass 1: 4, pass 2: 3, pass 3: 2, pass 4: 1 |
| Total: 4 + 3 + 2 + 1 = 10 comparisons |
Selection sort: the idea
| Find the smallest value in the unsorted part |
| Swap it into the first unsorted position |
| The sorted part grows by one each pass |
| Only one swap per pass |
Selection sort pass by pass
| Pass | Smallest | Array after |
|---|---|---|
| Start | none | 29 10 14 37 13 |
| Pass 1 | 10 | 10 29 14 37 13 |
| Pass 2 | 13 | 10 13 14 37 29 |
| Pass 3 | 14 | 10 13 14 37 29 |
| Pass 4 | 29 | 10 13 14 29 37 |
Quick answers
Why do you need a temporary variable to swap?
Without it, the first assignment overwrites and loses the old value.
How many comparisons does bubble sort make for 5 values?
10: four, then three, then two, then one.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
Why is it called bubble sort?40 sec
Can a sort swap only once per pass?39 sec
What is insertion sort like?37 sec
Which sort makes the fewest swaps?40 secThe full lesson, in text
Hello students, welcome to Kwickprep. Your teacher wants the marks list arranged from lowest to highest. How does a computer put numbers in order? Today we learn bubble sort, selection sort and insertion sort, pass by pass, and compare all three.
Let us begin with a few words. Sorting means arranging values in a fixed order. Ascending order goes from smallest to largest, like one, two, three. Descending order goes from largest to smallest, like a ranking list. A pass means one full round through the array, and a sort needs several passes.
Every sort needs to swap two values, which means exchange them. You cannot just write P equals Q, because the old value of P would be lost. So we use a third variable T, short for temporary, like an empty glass. T keeps P, then P takes Q, and then Q takes T. Now P is nine and Q is five.
Bubble sort works by comparing neighbours. It compares each element with the one just after it. If the left one is bigger, the two are swapped. After one pass, the largest value has moved to the end, like a bubble rising to the top. So for N values, at most N minus one passes are needed.
Let us sort five, one, four, two. At the start, nothing is in its final place. In pass one, five is swapped with one, then with four, then with two, so five reaches the end. In pass two, one and four stay, four and two swap, so four is now fixed too. In pass three, no swaps happen, and the array is fully sorted.
Here is the core of bubble sort, where A is the array and N is its length. The outer loop counts the passes, which is N minus one passes. The inner loop compares neighbours, J and J plus one. Each pass can stop one place earlier, because the end is already sorted. If the left value is bigger, the three lines swap them using T. To sort in descending order, just change greater than to less than.
Pause and predict. For an array of five values, how many comparisons does bubble sort make in total? Pass one makes four comparisons, pass two makes three, then two, then one. So the total is ten comparisons, even if the array was already sorted.
Selection sort is like picking the shortest student first for a class photo line. In each pass, it finds the smallest value in the unsorted part. Then it swaps that value into the first unsorted position. So the sorted part at the front grows by one after every pass. Unlike bubble sort, it does at most one swap in each pass.
Let us sort twenty nine, ten, fourteen, thirty seven and thirteen. This is the starting array. In pass one, the smallest is ten, so it swaps with twenty nine at the front. In pass two, the smallest of the rest is thirteen, so it swaps with twenty nine. In pass three, fourteen is already in the right place, so nothing moves. In pass four, twenty nine swaps with thirty seven, and the array is sorted.
Here is the core of selection sort. The outer loop picks the position to fill, starting from index zero. M stores the index of the smallest value found so far. The inner loop checks the rest of the array, and updates M when it finds a smaller value. After the inner loop, one swap puts the smallest value into its place.
Insertion sort is taught in the higher classes of some boards, and it is easy to picture. Think of arranging playing cards in your hand, one card at a time. We take the next value and call it the key. Every bigger value in the sorted part moves one step to the right. Then the key drops into the gap that is left.
Let us sort five, two, four, one. At the start, the first value five is treated as a sorted part of one. In pass one, the key is two. Five shifts right, and two goes in front. In pass two, the key is four. Five shifts right, and four sits between two and five. In pass three, the key is one. Five, four and two all shift right, and one goes to the front.
Here is the core of insertion sort. The outer loop starts at index one, because one value alone is already sorted. Key is the value to insert, and J starts just before it. The while loop shifts each bigger value one step right, and moves J to the left. It stops at the start of the array, or at a value that is not bigger. Finally, the key goes into position J plus one.
Now let us compare the three sorts. Bubble sort swaps neighbours again and again, so it can make many swaps. Selection sort picks the smallest value and makes only one swap in each pass. Insertion sort shifts values and inserts the key, and it is very fast when the array is almost sorted. For large arrays, all three become slow, because the work grows with N times N.
Some exams ask for bubble sort in pseudocode. This shows one pass over an array of five values, where the first index is one. If a value is bigger than the next one, the three lines swap them using Temp. The loop closes with next J, and an outer loop repeats this pass four times.
Let us revise. To swap two values, use a temporary variable. Bubble sort swaps neighbours, and the largest value reaches the end in each pass. Selection sort finds the smallest value and makes one swap per pass. Insertion sort shifts bigger values right and inserts the key. And N values need N minus one passes. Trace each sort on paper with your own numbers.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 11 Information Technology (802) | Part B, Unit 3: Office Automation Tools |
| ICSE Class 10 Computer Applications | Arrays |
| ISC Class 11 Computer Science (868) | Arrays and Strings |
| ISC Class 12 Computer Science (868) | Arrays and Strings |
| GSEB Std 11 Computer Studies | Basic Ubuntu Linux Commands |
| Cambridge IGCSE Grade 10 Computer Science (0478) | 7. Algorithm Design and Problem-Solving |
| NIOS Senior Secondary Data Entry Operations (336) | Lesson 7: Formatting of Spreadsheets |
| Programming All levels Java | Arrays and Strings |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

