Bubble Sort Algorithm with practical program in C

Article by: Manish Methani

Last Updated: October 10, 2021 at 10:04am IST
5 min 42 sec read

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 the 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, a 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, a swap takes place.

After swapping 8 & 4, the 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, a swap takes place.

After swapping 8 & 2, the 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, a swap takes place.

After swapping 5 & 4, the 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, a swap takes place.

After swapping 5 & 2, the 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, a swap takes place.

After swapping 5 & 4, the 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 a total of 4 passes was required to sort the array of 5 elements. In the first pass, 4 comparisons were done and the largest element was bubbled out at the end of the array. Similarly, in the second pass, 3 comparisons were done, in the third pass, 2 comparisons 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 the first pass, N-1 comparisons will be done, in the second pass, N-2 comparisons will be made, in the third pass, N-3 comparisons and so on until only 1 comparison is needed.

So total number of comparisons

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

Thus, the complexity of bubble sort in the 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 

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 :	", 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	", arr[i]);
    }
    printf("
");
    printf("
");

  }
}

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	", arr[i]);
  }

  printf("

");

  bubbleSort(N, arr);

  printf("Sortrd Array: 	");

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

  printf("
");

  return 0;
}

Explore Tech Guide:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com