How it works...

We will start by defining a macro, max, of value 20. You can always increase the value of max as required. Then, we will define an array, arr, of size max, that is, of size 20. You will be asked how many values you want to sort. Assuming that you want to sort seven elements, the value you entered will be assigned to the len variable. You will be prompted to enter the values to be sorted, which will then be assigned to the arr array. The seven values to be sorted in the arr array might appear as follows:

Figure 9.7

Now, we will run two nested for loops: the outer for loop will execute from len-2, that is, from value 5 to 1 in descending order, and the inner for loop will execute for the value from 0 to i. That means, in the first iteration, the value of i will be 5, so the inner for j loop will execute from 0 to 5. Within the inner for loop, the first value of arr will be compared with the second, the second value with the third, and so on:

Figure 9.8

The tendency is to keep the value at the lower index smaller than the value at the higher index. If the first value is larger than the second, they will change places; and if the first value is already smaller than the second value, then the next two values in line, that is, the second and third values, are taken for consideration. Similarly, if the second value is larger than the third, they too will swap places; if not, then the next set of values, that is, the third and fourth values, will be compared. The process will continue until the last pair, that is, the sixth and seventh values in our case, are compared.

The entire first iteration of comparisons is illustrated as follows:

Figure 9.9

You can see that after the first iteration, the largest value has bubbled down to the bottom of the list. Now, the value of the outer loop, that is, the value of i will be decremented by 1, making it 4. Consequently, the value of j in the inner loop will make the for loop run from value 0 to 4. It also means that now, the first value will be compared with the second, the second with the third, and so on. Finally, the fifth value (that is, the value at index location 4) will be compared with the sixth value (that is, the value at index location 5). The last element at index location 6 will not be compared as it is already at its correct destination:

Figure 9.10

Again, after the second iteration, the value of the outer loop will be decremented by 1, making it 3. As a result, the value of j in the inner loop will make the for loop run from value 0 to 3. In the last, the fourth value, that is, the value at index location 3, will be compared with the fifth value. The last two elements at index location 5 and 6 are not compared as both are at their correct destination:

Figure 9.11

After the third iteration, the value of i will be decremented by 1, making it 2. Hence, the value of j will make the for loop run from value 0 to 2. The last three elements at index location 4, 5, and 6 are not compared as they already are at their correct destination:

Figure 9.12

After the fourth iteration, the value of i will be decremented again, making it 1. So, the value of j in the inner loop will make the for loop run from value 0 to 1. The last four elements are not compared as they already are at their final destination:

Figure 9.13

So, after five iterations, we have successfully arranged the numbers in our array in ascending order. The program is compiled using GCC with the following statement:

gcc bubblesort.c -o bubblesort

Because no error appears on compilation, that means the bubblesort.c program has successfully been compiled into the bubblesort.exe file. On executing this file, it will ask us to specify how many numbers there are to be sorted.  Then the program will prompt us to enter the numbers to be sorted. After entering the numbers, they will appear sorted in ascending order, as shown in the following screenshot:

Figure 9.14

Voilà! We've successfully used the bubble sort technique to arrange numbers in ascending order.

Now let's move on to the next recipe!