position: fixed; top: auto !important; margin-left: 112px;

# Bubble Sort

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.

Suppose we have to sort the array of elements(1,8,5,4,2) in ascending order using Bubble Sort:

## 1) First pass:

1) Array is 1, 8, 5, 4, 2 .

In given array first two elements are compared 1 & 8 and since 1 < 8, no swapping takes place. 2) Array becomes 1, 8, 5, 4, 2 .

Now the next two elements are compared 8 & 5 and since 8 > 5, swap takes place. After swapping 8 & 5 array looks like this 1,5,8,4,2. 3) Array becomes 1, 5, 8, 4, 2.

Now the next two elements are compared 8 & 4 as and since 8 > 4, swap takes place. After swapping 8 & 4, array looks like this 1,5,4,8,2. 4) Array becomes 1, 5, 4, 8, 2.

Now the next two elements are compared 8 & 2 as and since 8 > 2, swap takes place. After swapping 8 & 2, array looks like this 1, 5, 4, 2, 8. ## 2) Second pass:

1) Array becomes 1, 5, 4, 2, 8.

Now the next two elements are compared 1 & 5 and since 1 < 5, no swap here. 2) Array becomes 1, 5, 4, 2, 8.

Now the next two elements are compared 5 & 4 as and since 5 > 4, swap takes place. After swapping 5 & 4, array looks like this 1, 4, 5, 2, 8. 3) Array becomes 1, 4, 5, 2, 8.

Now the next two elements are compared 5 & 2 as and since 5 > 2, swap takes place. After swapping 5 & 2, array looks like this 1, 4, 2, 5, 8. ## 3) Third pass:

1) Array becomes 1, 4, 2, 5, 8.

Now the next two elements are compared 1 & 4 and since 1 < 4, no swap here. 2) Array becomes 1, 4, 2, 5, 8.

Now the next two elements are compared 4 & 2 as and since 4 > 2, swap takes place. After swapping 5 & 4, array looks like this 1, 2, 4, 5, 8. ## 4) Fourth pass:

1) Array becomes 1, 2, 4, 5, 8.

Now the next two elements are compared 1 & 2 and since 1 < 2, no swap here. ## The complexity of Bubble Sort:

Notice that total 4 passes were required to sort the array of 5 elements. In first pass, 4 comparisons were done and largest element was bubbled out at the end of the array. Similarly, in second pass, 3 comparisions were done, in the third pass, 2 comparisions were done and so on... At the end of each pass, the largest element gets bubbled up near the end of the array.

In general,

To bubble sort an array of N elements, total N-1 passes are required in the worst case.

In first pass N-1 comparisons will be done, in second pass, N-2 comparisions will be made, in third pass, N-3 comparisons and so on until only 1 comparisons is needed.

So total number of comparisions

= (N-1) + (N-2) + (N-3) + .... + 1 = (N-1)(N-2) / 2 = O(N2) ....ignoring constants and lower order terms..

Thus, complexity of bubble sort in worst case is O(N2) where N is the number of elements of the array.

## Bubble Sort Program in C:

This basic Bubble Sort in C programming explains the concept of how to sort the elements using bubble sort in Data Structures.

```#include <stdio.h>

void swap(int *a, int *b){
*a = *a + *b;
*b = *a - *b;
*a = *a -*b;
}

void bubbleSort(int N, int arr[]) {
int pass = 0;
int flag = 1;
int i;

for(pass = 0; pass < N && flag; pass++)
{

printf("Pass %d :\t", pass);

for (i = 0; i < N - pass -1; ++i)
{
if (arr[i] > arr[i+1])
{
swap(&arr[i], &arr[i+1]);
flag = 1;
}
else{
flag = 0;+
}
}

for (i = 0; i < N; ++i)
{

printf("%d\t", arr[i]);
}
printf("\n");
printf("\n");

}
}

int main(void) {

int N;
scanf("%d", &N);
int arr[N], i;
for(i = 0; i < N; i++) {
scanf("%d", &arr[i]);
}

printf("Input Array is: ");

for(i = 0; i < N; i++) {
printf("%d\t", arr[i]);
}

printf("\n\n");

bubbleSort(N, arr);

printf("Sortrd Array: \t");

for(i = 0; i < N; i++) {
printf("%d\t", arr[i]);
}

printf("\n");

return 0;
}```

## Solve the Quiz of Article

1) While implementing the bubble sort algorithm, what do you think algorithm needs n - 1 comparison in Pass 1?
Yes
No

2) What do you think best case performance of Bubble Sort Algorithms is O(n2)?
Yes
No

## codzify.com

Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.