Home »
C/C++ Data Structure programs
C program to implement bubble sort
Bubble bort implementation in C: In this tutorial, we will learn how to implement bubble sort using C program?
By Sneha Dujaniya Last updated : August 03, 2023
Bubble Sort is a simple, stable, and in-place sorting algorithm.
- A stable sorting algorithm is the one where two keys having equal values appear in the same order in the sorted output array as it is present in the input unsorted array.
- An in-place sorting algorithm has various definitions but a more used one is – An in-place sorting algorithm does not need extra space and uses the constant memory for manipulation of the input in-place. Although, it may require some extra constant space allowed for variables.
Due to its simplicity, it is widely used as a sorting algorithm by computer programmers.
The basic working principle of bubble sort is that it repeatedly swaps the adjacent elements if they are in the wrong order. Hence, after every full iteration, the largest element reaches its position as in the sorted array.
Pseudo code
1. for i: 0 to n-1 not inclusive do:
2. for j: 0 to n-i-1 not inclusive do:
3. If a[j] > a[j+1] then
4. swap a[j] and a[j+1]
5. end if
6. end for
7. end for
Example with explanation
Input Array:
5 8 1 2 9
Here, I will run from 0 to 3
Since, i < n-1 => i < 5-1 => i < 4
Iteration 1 (i = 0):
For j = 0, (5 8 1 2 9) -> (5 8 1 2 9) No swap because 5 < 8
For j = 1, (5 8 1 2 9) -> (5 1 8 2 9), swap because 1 < 8
For j = 2, (5 1 8 2 9) -> (5 1 2 8 9), swap because 2 < 8
For j = 3, (5 1 2 8 9) -> (5 1 2 8 9), no swap
1st Pass gives – 5 1 2 8 9
Iteration 2 (i = 1):
For j = 0, (5 1 2 8 9) -> (1 5 2 8 9) No swap because 1 < 5
For j = 1, (1 5 2 8 9) -> (1 2 5 8 9), swap because 2 < 5
For j = 2, (1 2 5 8 9) -> (1 2 5 8 9), no swap
2nd Pass gives – 1 2 5 8 9
Iteration 3 (i = 2):
For j = 0, (1 2 5 8 9) -> (1 2 5 8 9), No swap because 1 < 2
For j = 1, (1 2 5 8 9) -> (1 2 5 8 9), No swap 2 < 5
3rd Pass gives – 1 2 5 8 9
Iteration 4 (i = 3):
For j = 0, (1 2 5 8 9) -> (1 2 5 8 9), No swap because 1 < 2
4th Pass gives – 1 2 5 8 9 because, last element is automatically sorted.
Time complexity
The time complexity of Binary Search can be described as: T(n) = T(n/2) + C
- Worst case: O(n^2)
- Average Case: O(n^2)
- Best case: O(n^2), since the loops run even if the array is sorted
- Space Complexity: O(1)
This simple implementation of Bubble Sort is just to explain the concept of the algorithm. In real life, whenever bubble sort is used, the optimized version is preferred over this one. That is because the optimized version gives O(n) time complexity in the best case.
In the optimized bubble sort, the inner loop doesn't run if the array is already sorted. Whereas, in the simple implementation, no such provision is there.
C program to implement bubble sort
#include <stdio.h>
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void bubble_sort(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]);
}
int main()
{
int arr[] = { 12, 46, 34, 82, 10, 9, 28 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("\nInput Array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
bubble_sort(arr, n);
printf("\nSorted Array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output
Input Array:
12 46 34 82 10 9 28
Sorted Array:
9 10 12 28 34 46 82