Sorting — Pseudocode

Bubble sort puts a list in order by swapping. Compare every pair of neighbouring items with IF numbers[i] > numbers[i + 1], and swap the two through a third…

Here are Mrs. Sri's notes on this morning's order queue, in the order the WhatsApp messages were read:

08:05 Dodi
07:50 Rina
08:20 Sari
07:55 Bayu

Mrs. Sri wants the customer who came first to be served first. By the end of this lesson, the program you write can put those notes in order, like this:

07:50 Rina
07:55 Bayu
08:05 Dodi
08:20 Sari

It takes only one small move, repeated again and again: compare two items of the list that are next to each other, and swap them if they are in the wrong order. We start with a list of numbers. Times and names come in the last section.

Swapping two items

In the Variables lesson, swapping what two variables hold needed a third variable to keep one of the values for a moment. Swapping two items of a list works the same way. This program swaps 31 and 4, and the result is below the code:

numbers ← [31, 4]
temp ← numbers[0]
numbers[0] ← numbers[1]
numbers[1] ← temp
PRINT numbers
[4, 31]

temp holds 31 first. Then numbers[0] is set to 4. Last, the 31 kept in temp is copied into numbers[1].

One pass: compare the neighbours

Now the list holds five numbers. Compare item 0 with item 1, then item 1 with item 2, and so on to the end of the list. Each time the item in front is bigger than the item behind it, swap the two. Going through the list once like this is called one pass. This program runs one pass, and prints the list after each comparison:

numbers ← [31, 4, 23, 9, 17]
FOR i ← 0 TO LENGTH(numbers) - 2
    IF numbers[i] > numbers[i + 1] THEN
        temp ← numbers[i]
        numbers[i] ← numbers[i + 1]
        numbers[i + 1] ← temp
    END IF
    PRINT numbers
END FOR
[4, 31, 23, 9, 17]
[4, 23, 31, 9, 17]
[4, 23, 9, 31, 17]
[4, 23, 9, 17, 31]

i stops at LENGTH(numbers) - 2, which is 3, because each comparison also looks at numbers[i + 1], the next item. When i holds 3, the next item is numbers[4], the last item.

This trace table records that pass, one row for each value of i:

i Compared Swapped? List afterwards
0 31 > 4 yes [4, 31, 23, 9, 17]
1 31 > 23 yes [4, 23, 31, 9, 17]
2 31 > 9 yes [4, 23, 9, 31, 17]
3 31 > 17 yes [4, 23, 9, 17, 31]

The biggest number, 31, is swapped again and again until it reaches the end of the list, like a bubble rising to the surface of water. That is why this way of sorting is called bubble sort. But the other numbers are not in order yet: 23 is still in front of 9.

Repeat the pass

One pass puts the biggest number at the end of the list. The next pass puts the second biggest number just in front of it, and so on. So put the FOR loop from the previous section inside another FOR, which counts the passes:

numbers ← [31, 4, 23, 9, 17]
FOR pass ← 1 TO LENGTH(numbers) - 1
    FOR i ← 0 TO LENGTH(numbers) - 2
        IF numbers[i] > numbers[i + 1] THEN
            temp ← numbers[i]
            numbers[i] ← numbers[i + 1]
            numbers[i + 1] ← temp
        END IF
    END FOR
    PRINT "After pass", pass, numbers
END FOR
After pass 1 [4, 23, 9, 17, 31]
After pass 2 [4, 9, 17, 23, 31]
After pass 3 [4, 9, 17, 23, 31]
After pass 4 [4, 9, 17, 23, 31]

For this list, two passes are enough. But not every list is like that. Before you read on, guess: if the smallest number is at the very back, as in [9, 17, 23, 31, 4], how many passes does it take?

Four passes. In each pass, the 4 only moves one place forward: [9, 17, 23, 4, 31], then [9, 17, 4, 23, 31], then [9, 4, 17, 23, 31], and at last [4, 9, 17, 23, 31]. So a list of five items needs four passes to be sure it is sorted, whatever order it starts in. That is why the outer loop runs up to LENGTH(numbers) - 1.

Text can be sorted too

< and > can compare text too. Two texts are compared letter by letter from the left, by each letter's code (the number the computer uses for that letter). The order is close to a dictionary's, with one difference: every capital letter comes before every lower-case letter, so "Zoe" < "adam" is TRUE. The text that comes first in that order counts as smaller:

PRINT "Bayu" < "Rina"
PRINT "07:50" < "08:05"
PRINT "7:50" < "10:05"
TRUE
TRUE
FALSE

Look at the third line. The first character of "7:50" is 7, and the first character of "10:05" is 1. In text order, 1 comes before 7, so "10:05" counts as the smaller text and the result is FALSE. This means 10 o'clock counts as earlier than 7 o'clock. So always write the hour with two digits, 07:50. Then the order of the text is the same as the order of the times.

So Mrs. Sri's queue notes can be sorted with exactly the same two loops as the list of numbers. In each item the time is written first, as in "07:50 Rina". That is why sorting the text also sorts by arrival time.

In JavaScript, sorting numbers is written numbers.sort((a, b) => a - b). Without (a, b) => a - b, JavaScript sorts numbers as text, and the result is [17, 23, 31, 4, 9]. This is the same trap as "7:50" and "10:05" above. In Python, sorted(numbers) is enough. The Bubble Sort lesson in the Algorithms category explains bubble sort further, and also faster ways to sort.

⚠️ Why is the order still a mess?

TL;DR: Bubble sort puts a list in order by swapping. Compare every pair of neighbouring items with IF numbers[i] > numbers[i + 1], and swap the two through a third variable, temp. One pass, which is going through the list once from the front to the end, carries the biggest number to the end. The next passes put the next biggest numbers in place, and the last number left is already at the front. So a list of n items needs n - 1 passes: FOR pass ← 1 TO LENGTH(numbers) - 1 outside, and FOR i ← 0 TO LENGTH(numbers) - 2 inside it. Text can be sorted too, because < and > compare text letter by letter from the left (capital letters before lower-case ones). For that to work, an hour inside the text must be written with two digits, such as 07:50.

⚡ Swapping two items

Write:

numbers ← [31, 4]
temp ← numbers[0]
numbers[0] ← numbers[1]
numbers[1] ← temp
PRINT numbers
Guess first: what if there is no temp, only numbers[0] ← numbers[1] then numbers[1] ← numbers[0]? What does numbers hold then?

[4, 4]. The first line replaces 31 with 4, so both items hold 4. The 31 is lost, with no error message.

Meaning: Like swapping what two variables hold, swapping two items of a list needs a third variable, here temp. temp holds 31 first. After numbers[0] is set to 4, that 31 is copied into numbers[1]. The result is [4, 31].

⚡ One pass: compare the neighbours

Write:

numbers ← [31, 4, 23, 9, 17]
FOR i ← 0 TO LENGTH(numbers) - 2
    IF numbers[i] > numbers[i + 1] THEN
        temp ← numbers[i]
        numbers[i] ← numbers[i + 1]
        numbers[i + 1] ← temp
    END IF
    PRINT numbers
END FOR

Meaning: This is one pass: going through the list once from the front to the end. Every pair of neighbouring items is compared, and the two are swapped when the one in front is bigger. The biggest number, 31, is swapped again and again until it reaches the end, like a bubble rising to the surface of water. That is why this is called bubble sort. i stops at LENGTH(numbers) - 2, because each comparison also looks at numbers[i + 1], the next item.

⚡ Repeat the pass

Guess first: how many passes does [9, 17, 23, 31, 4] need to be sorted?

Four. In each pass, the 4 only moves one place forward.

Meaning: One pass only makes sure that one number, the biggest, reaches its place. After 4 passes, the number that is left is already at the front, so a list of 5 items needs 4 passes to be sure it is sorted. To do that, put the FOR i loop from the previous step inside an outer loop, FOR pass ← 1 TO LENGTH(numbers) - 1.

⚡ Text can be sorted too

Meaning: < and > can compare text too, letter by letter from the left. The order is close to a dictionary's, but every capital letter comes before every lower-case letter. So a list of texts can be sorted the same way. Be careful with times: "7:50" < "10:05" is FALSE, because the first characters are 7 and 1, and 1 comes before 7. Write the hour with two digits, 07:50.

What you will learn