**Bubble Sort in c language :**

This is an example of c program to sort an array using Bubble Sort Algorithm. The elements of the array are entered by the user. Bubble Sort c program is explained properly with algorithm (logic) and example.

C program for Bubble Sort is used to sort array in ascending order.You can also sort the array in descending order by modifying the condition in loop.

** Topics covered**

- Bubble Sort
**Algorithm in c** - Bubble Sort c program
- Bubble Sort c program
**Explanation** - Bubble Sort c program using
**Function** - Bubble sort
**Time Complexity**in c

## Bubble Sort Algorithm in c

This explains step-by-step the logic how Bubble Sort in c language works.

- Bubble sort algorithm
**starts by comparing first two elements of the array**,i.e., 1st element and 2nd element of the array. **If 1st element is greater than the 2nd element then swap**(or interchange) the elements ,i.e., 1st element will come in place of 2nd element and 2nd element will come in place of 1st element.

If 1st element is smaller than the 2nd element then do not swap elements.- Similarly, 2nd and 3rd elements are compared and if
**2nd element is greater than 3rd element then swap**them otherwise do not swap. - This process will continue untill the second last and last elements of the array are compared. This is the
**first iteration or first loop**of bubble sort algorithm.

If there are n elements in the array then there will be (n-1) iteration or loop in the bubble sort algorithm.

**To make Bubble Sort Algorithm more efficient –**

- In Bubble Sort c program, as the largest element will be automatically placed at the last position in the array after 1st iteration. So, there is
**no need to compare second last and last element of the array in the 2nd iteration.** - Similarly, there is no need to compare last and second last and third last and second last elements.

So, there will be no need for nth iteration as all the elements in the array get sorted in the (n-1)th iteration in Bubble Sort c program.

## Bubble Sort c program

```
/* Bubble Sort in c */
#include <stdio.h> //printf() & scanf() Header File
#include <conio.h> //clrscr() & getch() Header File
int main()
{
int a[20], n, i, j, t;
clrscr(); // to clear output screen
/* To take number of elements in an array from user */
printf("Enter how many elements should be in the array: ");
scanf("%d", &n);
/* Array Input */
for (i=0; i<n; i++)
{
printf(Enter array element %d", i+1);
scanf("%d", &a[i]);
}
/* Array Sort (In Ascending Order) */
for (i=1; i<n; i++)
{
for (j=0; j<n-i; j++)
{
if (a[j]>a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
/* Print the sorted array */
printf("Sorted Array - ");
for (i=0; i<n; i++)
{
printf("%dt", a[i]);
}
getch(); //to hold output screen
return(0);
}
```

**Output –**

```
Enter how many elements should be in the array: 5
Enter array element 1: 40
Enter array element 2: 30
Enter array element 3: 10
Enter array element 4: 50
Enter array element 5: 20
Sorted Array - 10 20 30 40 50
```

### Bubble Sort C Program Explanation

The picture speaks more than a thousand words. So, the below image properly explains the First iteration of Bubble Sort in c.

## Bubble Sort c program using Function

Here, all the code for Bubble Sort c program is written in a user-defined function void bubble_sort(int) and this function is called in main().

```
/* Bubble Sort c program */
#include <stdio.h> //printf() & scanf() Header File
#include <conio.h> //clrscr() & getch() Header File
/* function declaration */
void bubble_sort(int);
int main()
{
int n;
clrscr(); // to clear output screen
/* To take number of elements in an array from user */
printf("Enter how many elements should be in the array: ");
scanf("%d", &n);
/* calling function */
bubble_sort(n);
getch(); //to hold output screen
return(0);
}
void bubble_sort(int n);
{
int a[20], i, j, t;
/* Array Input */
for (i=0; i<n; i++)
{
printf(Enter array element %d", i+1);
scanf("%d", &a[i]);
}
/* Array Sort (In Ascending Order) */
for (i=1; i<n; i++)
{
for (j=0; j<n-i; j++)
{
if (a[j]>a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
/* Print the sorted array */
printf("Sorted Array - ");
for (i=0; i<n; i++)
{
printf("%dt", a[i]);
}
}
```

**Output –**

```
Enter how many elements should be in the array: 5
Enter array element 1: 40
Enter array element 2: 30
Enter array element 3: 10
Enter array element 4: 50
Enter array element 5: 20
Sorted Array - 10 20 30 40 50
```

## Bubble Sort Time Complexity in c

In Bubble Sort c program Time Complexity, we will explain the **Worst case, Average case and Best case.**

**Best case Time Complexity:**

**O(n)** is the Best Case complexity for Bubble Sort c program. It occurs when **elements of the array are already sorted.** So, there are no exchanges and the performance of Bubble Sort c program increases.

**Worst case and Average case Time Complexity:**

**O(n*n)** is the Worst Case complexity for Bubble Sort c program. It occurs when **elements of array are in reverse order.** So, there are maximum number of exchanges and the performance of Bubble Sort c program decreases.

**Note:**

Bubble sort c program is the simplest and easy to implement for sorting array elements. As the logic remains same for all languages, Bubble Sort algorithm can be used in any language to sort array elements.

When number of elements in unsorted list increases, efficiency of Bubble Sort algorithm starts decreasing. so, avoid Bubble sort c program for arrays of large size ,i.e., having many elements.

Insertion sort algorithm is better than Bubble Sort algorithm for large size arrays due to its high efficiency.

**Other c programs examples:**