# Bubble Sort Code example in C & CPP – Understand the algorithm too  Bubble sort is also known as exchange sort. Bubble sort is a simplest sorting algorithm. In bubble sort algorithm array is traversed from 0 to the length-1 index of the array and compared one element to the next element and swap values in between if the next element is less than the previous element. In other words, bubble sorting algorithm compare two values and put the largest value at largest index. The algorithm follow the same steps repeatedly until the values of array is sorted. In worst-case the complexity of bubble sort is O(n2) and in best-case the complexity of bubble sort is Ω(n).

## Code description:

Bubble Sorting is an algorithm in which we are comparing first two values and put the larger one at higher index. Then we take next two values compare these values and place larger value at higher index. This process do iteratively until the largest value is not reached at last index. Then start again from zero index up to n-1 index. The algorithm follows the same steps iteratively unlit elements are not sorted.

## Working of bubble sort algorithm:

Say we have an array unsorted A,A,A……………. A[n-1] and A[n] as input. Then the following steps are followed by bubble sort algorithm to sort the values of an array.
1.Compare A and A .
2.If A>A then Swap A and A.
3.Take next A and A.
4.Comapre these values.
5.If A>A then swap A and A
………………………………………………………
……………………………………………………….
at last compare A[n-1] and A[n]. If A[n-1]>A[n] then swap A[n-1] and A[n]. As we see the highest value is reached at nth position. At next iteration leave nth value. Then apply the same steps repeatedly on A,A,A……………. A[n-1] elements repeatedly until the values of array is sorted.

In our example we are taking the following array values
12 9 4 99 120 1 3 10

The basic steps followed by algorithm:-

In the first step compare first two values 12 and 9.
12 9 4 99 120 1 3 10

As 12>9 then we have to swap these values
Then the new sequence will be
9 12 4 99 120 1 3 10

In next step take next two values 12 and 4
9 12 4 99 120 1 3 10

Compare these two values .As 12>4 then we have to swap these values.
Then the new sequence will be
9 4 12 99 120 1 3 10

We have to follow similar steps up to end of array. e.g.
9 4 12 99 120 1 3 10
9 4 12 99 120 1 3 10
9 4 12 99 1 120 3 10
9 4 12 99 1 120 3 10
9 4 12 99 1 3 120 10
9 4 12 99 1 3 10 120

When we reached at last index .Then restart same steps unlit the data is not sorted.

The output of this example will be :
1 3 4 9 10 12 99 120

## Source Code

### Program Without External Function:

```/* Bubble sort code */

#include<stdio.h>
#include<stdlib.h>

int main() {
int array, n, c, d, swap;

printf("Enter number of elements\n");
scanf("%d", &n);

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

for (c = 0; c < n; c++) //the numbers are supplied in any order
scanf("%d", &array);

for (c = 0; c < (n - 1); c++) { //outer loop starts
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d + 1]) { // For decreasing order use <
swap = array[d]; //swap using tempoprary variable
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}

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

for (c = 0; c < n; c++)
printf("%d\n", array); //printing the sorted array

return 0;
}

```

### Program Using Function:

```/* Bubble sort code */

#include<stdio.h>
#include<stdlib.h>

//bubble sort using function

void bubble_sort(int [], int); //function definition

int main() {
int array, n, c, d, swap;

printf("Enter number of elements\n");
scanf("%d", &n);

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

for (c = 0; c < n; c++)
scanf("%d", &array);

bubble_sort(array, n);

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

for (c = 0; c < n; c++)
printf("%d\n", array);

return 0;
}

void bubble_sort(int list[], int n) //function body
{
int c, d, t;

for (c = 0; c < (n - 1); c++) //outer loop
{
for (d = 0; d < n - c - 1; d++) //inner loop
{
if (list[d] > list[d + 1]) {
/* Swapping */

t = list[d]; //using temporary variable
list[d] = list[d + 1];
list[d + 1] = t;
}
}
}
}
```

## Output: Categories
Previous article

Next article

## 10 C programs you must know before appearing for technical interviews

1. 