Sort an Array using Bubble Sort in C

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until the array is fully sorted. In this tutorial, we will learn how to implement Bubble Sort Algorithm in C with multiple examples.


Example 1: Sorting an Integer Array in Ascending Order

In this example, we will implement the Bubble Sort algorithm to sort an array of integers in ascending order.

main.c

</>
Copy
#include <stdio.h>

// Function to perform Bubble Sort
void bubbleSort(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

// Function to print the array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int numbers[] = {5, 2, 9, 1, 5, 6};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    // Sorting the array
    bubbleSort(numbers, size);

    // Printing the sorted array
    printArray(numbers, size);

    return 0;
}

Explanation:

  1. We define a function bubbleSort() that takes an array and its size as parameters.
  2. Two nested loops iterate through the array, comparing adjacent elements. Reference: Nested Loops.
  3. If an element is greater than the next element, they are swapped.
  4. The process is repeated until the largest elements “bubble” to the end.
  5. The printArray() function prints the sorted array.
  6. In main(), we initialize an array, determine its size, call bubbleSort(), and print the sorted array.

Output:

1 2 5 5 6 9

Example 2: Sorting an Integer Array in Descending Order

In this example, we modify the Bubble Sort algorithm to sort an array in descending order.

main.c

</>
Copy
#include <stdio.h>

// Function to perform Bubble Sort in descending order
void bubbleSortDescending(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j] < arr[j + 1]) {
                // Swap elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int numbers[] = {3, 7, 4, 9, 1, 5};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    // Sorting the array in descending order
    bubbleSortDescending(numbers, size);

    // Printing the sorted array
    for (int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    
    return 0;
}

Explanation:

  1. We define bubbleSortDescending() to sort the array in descending order.
  2. The nested loops compare adjacent elements.
  3. If an element is smaller than the next element, we swap them.
  4. The process repeats until the array is fully sorted in descending order.

Output:

9 7 5 4 3 1

Example 3: Optimized Bubble Sort (Early Termination)

We improve Bubble Sort by stopping early if no swaps occur in a pass, reducing unnecessary iterations.

main.c

</>
Copy
#include <stdio.h>
#include <stdbool.h>

// Optimized Bubble Sort with early termination
void optimizedBubbleSort(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        bool swapped = false;
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = true;
            }
        }
        if (!swapped) {
            break;
        }
    }
}

int main() {
    int numbers[] = {1, 2, 3, 4, 5, 6};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    // Sorting the already sorted array
    optimizedBubbleSort(numbers, size);

    // Printing the sorted array
    for (int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Explanation:

  1. We use a bool swapped variable to check if any swaps occur in a pass.
  2. If no swaps occur, the array is already sorted, so we break early.
  3. This reduces unnecessary comparisons, improving efficiency.

Output:

1 2 3 4 5 6