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.
- Set
n
with length of arrayarr
. - For each index
i
in the arrayarr
:- For each index
j
in the arrayarr
:- If
arr[j]
is greater thanarr[j+1]
, then swaparr[j]
witharr[j+1]
.
- If
- For each index
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.
- Set
n
with length of arrayarr
. - For each index
i
in the arrayarr
:- For each index
j
in the arrayarr
untilj
is less thann-i-1
:- If
arr[j]
is greater thanarr[j+1]
, then swaparr[j]
witharr[j+1]
.
- If
- For each index
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.
- Set
n
with length of arrayarr
. - For each index
i
in the arrayarr
:- For each index
j
in the arrayarr
untilj
is less thann-i-1
:- If
arr[j]
is less thanarr[j+1]
, then swaparr[j]
witharr[j+1]
.
- If
- For each index
C Program
In the following program, we take an array of integers, and sort them in ascending order using Bubble Sort.
main.c
#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
#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.