PPS Lab · Experiment 44
Bubble sort
CSorting and searchingJNTUH R22 · CS108ES
Aim
To write a C program that sorts a list of integers in ascending order using the bubble sort method, and shows the list after each pass.
Algorithm
- Read n (1 to 100) and then n integers into an array.
- Repeat for pass = 1 to n − 1:
- Compare each pair of neighbours a[j] and a[j + 1], for j from 0 to n − pass − 1. If a[j] is greater than a[j + 1], swap them.
- After a pass, the largest of the unsorted numbers has moved to its final place at the end. Print the list.
- If no swap happened in a pass, the list is already sorted, so stop early.
- Print the sorted list.
Program
#include <stdio.h>
#define MAX 100
void print_array(const int a[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
/* Sorts a[0..n-1] in ascending order, printing the array after each pass */
void bubble_sort(int a[], int n) {
for (int pass = 1; pass < n; pass++) {
int swapped = 0;
for (int j = 0; j < n - pass; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
swapped = 1;
}
}
printf("After pass %d: ", pass);
print_array(a, n);
if (swapped == 0) {
break; /* no swaps: the list is already sorted */
}
}
}
int main(void) {
int a[MAX], n;
printf("Enter the number of elements (1 to %d): ", MAX);
if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
printf("Invalid number of elements\n");
return 1;
}
printf("Enter %d integers: ", n);
for (int i = 0; i < n; i++) {
if (scanf("%d", &a[i]) != 1) {
printf("Invalid input\n");
return 1;
}
}
bubble_sort(a, n);
printf("Sorted list: ");
print_array(a, n);
return 0;
}Output
Enter the number of elements (1 to 100): 6
Enter 6 integers: 29 10 14 37 13 45
After pass 1: 10 14 29 13 37 45
After pass 2: 10 14 13 29 37 45
After pass 3: 10 13 14 29 37 45
After pass 4: 10 13 14 29 37 45
Sorted list: 10 13 14 29 37 45 Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
In each pass the largest remaining number "bubbles up" to the end, the way a bubble rises in water. That is why pass number pass only needs to compare up to index n - pass - 1: the last pass - 1 numbers are already in place.
The swapped flag stops the sort as soon as a pass makes no swaps. In the run, the list is sorted after pass 3, so pass 4 makes no swaps and the sort stops without doing pass 5. For a list that is already sorted, only one pass is needed.
Common mistakes
Running the inner loop up to
j < n. Thena[j + 1]readsa[n], which is outside the array. The inner loop must stop atn - pass - 1(or at leastn - 2).Swapping without a temporary variable, as in
a[j] = a[j + 1]; a[j + 1] = a[j];. Both places end up with the same value. Save one value intempfirst.Using
<instead of>in the comparison. That sorts the list in descending order.Setting
swapped = 0once before the outer loop instead of at the start of every pass. Then it stays 1 after the first swap and the early stop never happens.
Viva questions
Try to answer each one aloud before you open it.
How does bubble sort work?
It compares neighbouring elements and swaps them if they are in the wrong order. After each pass, the largest unsorted element has moved to its correct place at the end.
What is the time complexity of bubble sort?
O(n²) in the worst and average cases. With the
swappedflag, the best case (an already sorted list) is O(n).How many passes are needed at most for n elements?
n − 1 passes. After n − 1 elements are in their places, the last one is in place too.
Is bubble sort a stable sort?
Yes. It only swaps when
a[j] > a[j + 1], so equal elements never pass each other and keep their original order.Does bubble sort need extra memory?
Only one temporary variable for swapping, so the extra space is O(1). It sorts the array in place.
Learn the concepts
- Arrays in C: Declaration, Initialisation, Indexing and Common Operations
- Functions in C: Declaration, Definition, Prototypes and Calling
- for Loop in C: Syntax, Examples, Counting Down and Looping Over Arrays
- Bubble Sort, Selection Sort and Insertion Sort in Python
- How to Analyse Time Complexity of Code (Step by Step)