# C program to sort integers using Bubble Sort

«
»

This is a C Program to implement Bubble Sort Algorithm.

Problem Description

We have to input an array of numbers and sort them using Bubble Sort algorithm in C Language.

Expected Input and Output

1. Average case (Unsorted array): When the input array has random distribution of numbers.

For example:

If the input array is {4, 6, 1, 2, 5, 3}
the expected output array will have data as {1, 2, 3, 4, 5, 6}

2. Best case (Sorted Array): When the input array is already sorted, in that case we have to make minimum number of swaps.

Note: Join free Sanfoundry classes at Telegram or Youtube

For example:

If the input array has data as {-3, 31, 66}
then the expected output array will have data as {-3, 31, 66}

3. Worst Case (Reverse sorted array): When the array is sorted in reverse manner, in that case we have to make maximum number of swaps.

Take C Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

For example:

If the input array has elements as {9, 8, 6, 3, 1}
then the output array will have data as {1, 3, 6, 8, 9}
Problem Solution

1. In Bubble sort algorithm we compare the first two elements of an array and swap them if required.
2. If we want to sort the elements of array in ascending order and if the first element is greater than second then, we need to swap the elements.
3. If the first element is smaller than second, we don’t need to swap the elements. This process go on until last and second last element is compared and swapped.
For example:

If we have the array as {40,10,50,70,30}
and we apply bubble sort to sort the array,
then the resultant array after each iteration will be as follows:

Original array: {40, 10, 50, 70, 30}

Array after first iteration          10  ->   40   ->   50   ->   30   ->   70
Array after second iteration         10  ->   40   ->   30   ->   50   ->   70
Array after third iteration          10  ->   30   ->   40   ->   50   ->   70
Array after fourth iteration         10  ->   30   ->   40   ->   50   ->   70

Sorted array is  10  30  40  50  70
Program/Source Code

Here is source code of the C Program to sort an array of integers using Bubble Sort Algorithm. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown below.

1. /* C program to implement bubble sort */
2. #include <stdio.h>
3. /*
4.  * Main Function
5.  */
6. int main()
7. {
8.     int  n, j, i, swap;
9.     printf("Enter number of elements\n");
10.     scanf("%d", &n);
11.     int array[n];
12.     printf("Enter %d integers\n", n);
13.     for (i= 0; i < n; i++)
14.     {
15.         scanf("%d", &array[i]);
16.     }
17.     for (i = 0 ; i < n - 1; i++)
18.     {
19.         for (j = 0 ; j < n - i- 1; j++)
20.         {
21.             if (array[j] > array[j+1])
22.             {
23.                 swap       = array[j];
24.                 array[j]   = array[j+1];
25.                 array[j+1] = swap;
26.             }
27.         }
28.     }
29.
30.     printf("Sorted list in ascending order:\n");
31.
32.     for (i = 0; i < n; i++)
33.         printf("%d\n", array[i]);
34.     return 0;
35. }
Program Explanation

1. If the given array has to be sorted in ascending order, then bubble sort will start by comparing the first element of the array with the second element, if the first element is greater than the second element, it will swap both the elements, and then move on to compare the second and the third element, and so on.
2. This will go on till n – 1 times if we have a total of n elements in an array.
3. It is known as bubble sort, because with every complete iteration the largest element in the given array, bubbles up towards the highest index, just like a water bubble rises up to the water surface.

Runtime Test Cases
1. Enter number of elements
6
Enter 6 integers
4
6
1
2
5
3
Sorted list in ascending order:
1
2
3
4
5
6

2. Enter number of elements
3
Enter 3 integers
-3
31
66
Sorted list in ascending order:
-3
31
66

3. Enter number of elements
5
Enter 5 integers
9
8
6
3
1
Sorted list in ascending order:
1
3
6
8
9

Sanfoundry Global Education & Learning Series – 1000 C Programs.