C Program – Bubble Sort

To sort given array of elements in C, we can implement Bubble Sort algorithm. In this tutorial, we will write a program where we sort an array in ascending or descending order using Bubble Sort.

Bubble Sort Algorithm

Bubble Sort Algorithm for sorting an array of elements in ascending order.

  1. Set n with length of array arr.
  2. For each index i in the array arr:
    1. For each index j in the array arr:
      1. If arr[j] is greater than arr[j+1], then swap arr[j] with arr[j+1].

We can decrease the number of iteration in the inner for loop by considering the fact that the elements with index greater than n - i are already in their sorted positions.

  1. Set n with length of array arr.
  2. For each index i in the array arr:
    1. For each index j in the array arr until j is less than n-i-1:
      1. If arr[j]is greater than arr[j+1], then swap arr[j] with arr[j+1].

The later algorithm is referred to as Advanced Bubble Sort algorithm. We shall use this algorithm to sort an array in ascending order.

To sort the array in descending order, use the following algorithm.

  1. Set n with length of array arr.
  2. For each index i in the array arr:
    1. For each index j in the array arr until j is less than n-i-1:
      1. If arr[j]is less than arr[j+1], then swap arr[j] with arr[j+1].

C Program

In the following program, we take an array of integers, and sort them in ascending order using Bubble Sort.

main.c

</>
Copy
#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void bubbleSort(int arr[], int n) {
    int i, j;
    for (i = 0; i < n-1; i++) {
        for (j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                swap(&arr[j], &arr[j+1]);
            }
        }
    }
}

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

int main() {
    int arr[] = {6, 4, 2, 1, 3, 5, 7};
    int n = sizeof arr / sizeof arr[0];
    printf("Input  Array:\n");
    printArray(arr, n);
    //sort the array using bubble sort
    bubbleSort(arr, n);
    printf("Sorted Array:\n");
    printArray(arr, n);
    return 0;
}

Output

Input  Array:
6 4 2 1 3 5 7 
Sorted Array:
1 2 3 4 5 6 7 
Program ended with exit code: 0

To sort the array in descending order, inside the bubbleSort() function, instead of checking if arr[j] > arr[j+1], check if arr[j] < arr[j+1].

In the following program, we sort given array of integers in descending order using Bubble Sort algorithm.

main.c

</>
Copy
#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void bubbleSort(int arr[], int n) {
    int i, j;
    for (i = 0; i < n-1; i++) {
        for (j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                swap(&arr[j], &arr[j+1]);
            }
        }
    }
}

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

int main() {
    int arr[] = {6, 4, 2, 1, 3, 5, 7};
    int n = sizeof arr / sizeof arr[0];
    printf("Input  Array:\n");
    printArray(arr, n);
    //sort the array using bubble sort
    bubbleSort(arr, n);
    printf("Sorted Array:\n");
    printArray(arr, n);
    return 0;
}

Output

Input  Array:
6 4 2 1 3 5 7 
Sorted Array:
7 6 5 4 3 2 1 
Program ended with exit code: 0

Conclusion

In this C Tutorial, we have written C programs to sort a given array of elements in ascending or descending order using Bubble Sort algorithm.