This is a C Program to implement Bubble Sort Algorithm.

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

**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.

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.

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}

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 -> 70Sorted array is 10 30 40 50 70

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.

`/* C program to implement bubble sort */`

`#include <stdio.h>`

`/*`

`* Main Function`

`*/`

int main()

`{`

int n, j, i, swap;

printf("Enter number of elements\n");

scanf("%d", &n);

int array[n];

printf("Enter %d integers\n", n);

for (i= 0; i < n; i++)

`{`

scanf("%d", &array[i]);

`}`

for (i = 0 ; i < n - 1; i++)

`{`

for (j = 0 ; j < n - i- 1; j++)

`{`

if (array[j] > array[j+1])

`{`

swap = array[j];

array[j] = array[j+1];

array[j+1] = swap;

`}`

`}`

`}`

printf("Sorted list in ascending order:\n");

for (i = 0; i < n; i++)

printf("%d\n", array[i]);

return 0;

`}`

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.

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.**

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.