A merit list, a telephone directory and a dictionary are useful only because they are in order. Arranging data in order is called sorting, and it is among the most frequently performed tasks in computing. This topic studies one sorting method, the bubble sort.

What Is Sorting?

Definition: Sorting. The process of arranging the elements of a collection in a definite order, such as ascending or descending.

Sorted data can be searched by binary search, and its median, minimum and maximum can be read off directly.

The Idea of Bubble Sort

Definition: Bubble sort. A sorting method that repeatedly compares adjacent elements and swaps them when they are in the wrong order, so that after each pass the largest remaining element reaches its final position.

In ascending order, the larger values gradually move towards the end of the array, like bubbles rising in water, which gives the method its name.

One Pass

In a single pass, the elements at indices 0 and 1 are compared and swapped if the first is larger. Then the elements at indices 1 and 2 are treated in the same way, and so on to the end. At its end, the largest value, 8, is in the last position.

All the Passes

Each pass places one more element in its final position, so the next pass can stop one position earlier. For n elements, n − 1 passes are sufficient. Table 4.8 shows the array after each pass.

Table 4.8: The array 5, 3, 8, 1 after each pass of bubble sort

Pass Comparisons Array after the pass Element now in place
1 3 3, 5, 1, 8 8
2 2 3, 1, 5, 8 5
3 1 1, 3, 5, 8 3

Swapping Two Elements

Two values cannot be exchanged by a[j] = a[j + 1]; a[j + 1] = a[j];, because the first statement destroys the old value of a[j]. A third variable is needed to hold one value temporarily:

temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;

The Algorithm

The method uses two nested loops. The outer loop counts the passes, from 1 to n − 1. The inner loop makes the comparisons of one pass; in pass number p it runs over the indices 0 to n − p − 1.

Efficiency

The first pass makes n − 1 comparisons, the second n − 2, and so on down to 1. The total is n(n − 1) / 2, which is roughly n² / 2. For 10 elements this is 45 comparisons, and for 1,000 elements it is 4,99,500. Bubble sort is therefore suitable only for small arrays, but its simplicity makes it the usual first sorting method to learn.

An Improvement

If a complete pass makes no swap, the array is already in order and the remaining passes are unnecessary. A variable can record whether any swap occurred during a pass, and the sort stops as soon as a pass ends without one.

Example 4.8: Bubble sort

Problem: Sort the array 5, 3, 8, 1 into ascending order by bubble sort, displaying the array after each pass.

Solution:

  1. The outer loop makes n − 1 passes.
  2. The inner loop compares adjacent elements and swaps them when the left one is larger.
  3. The array is displayed at the end of each pass.
#include <stdio.h>

int main(void)
{
    int a[4] = {5, 3, 8, 1};
    int n = 4, pass, j, k, temp;

    for (pass = 1; pass <= n - 1; pass++)
    {
        for (j = 0; j < n - pass; j++)
        {
            if (a[j] > a[j + 1])
            {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
        printf("After pass %d: ", pass);
        for (k = 0; k < n; k++)
        {
            printf("%d ", a[k]);
        }
        printf("\n");
    }
    return 0;
}

Output:

After pass 1: 3 5 1 8
After pass 2: 3 1 5 8
After pass 3: 1 3 5 8

Result: The output matches Table 4.8. The array is sorted after three passes, and a total of 3 + 2 + 1 = 6 comparisons were made.

Common mistakes

  • Swapping two elements without a temporary variable, which loses one of the values.
  • Letting the inner loop run to n − 1, so that a[j + 1] refers to an element beyond the array.
  • Using < instead of > in the comparison, which sorts in descending order.

Key takeaways

  • Bubble sort compares adjacent elements and swaps those in the wrong order.
  • After each pass the largest remaining element is in its final place.
  • n − 1 passes are sufficient for n elements.
  • About n² / 2 comparisons are made, so the method suits only small arrays.

Practice questions

  1. How many passes does bubble sort need for 6 elements?
  2. What is the array 4, 2, 7, 1 after the first pass of bubble sort in ascending order?
  3. Why is a temporary variable needed to swap two elements?

Answers to the practice questions

  1. At most 5 passes.
  2. 2, 4, 1, 7.
  3. Without it, the first assignment overwrites a value that is still needed.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV