Structurex


Bubble Sort

Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items.



How Bubble Sort Works?

We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and precise.

Bubble sort starts with very first two elements, comparing them to check which one is greater.

In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27.

We find that 27 is smaller than 33 and these two values must be swapped.

The new array should look like this −

Next we compare 33 and 35. We find that both are in already sorted positions.

Then we move to the next two values, 35 and 10.

We know then that 10 is smaller 35. Hence they are not sorted.

We swap these values. We find that we have reached the end of the array. After one iteration, the array should look like this −

To be precise, we are now showing how an array should look like after each iteration. After the second iteration, it should look like this −

Notice that after each iteration, at least one value moves at the end.

And when there's no swap required, bubble sorts learns that an array is completely sorted.

Implementation in C

#include <stdio.h>
        #include <stdbool.h>
        
        #define MAX 10
        
        int list[MAX] = {1,8,4,6,0,3,5,2,7,9};
        
        void display() {
           int i;
           printf("[");
          
           // navigate through all items 
           for(i = 0; i < MAX; i++) {
              printf("%d ",list[i]);
           }
          
           printf("]\n");
        }
        
        void bubbleSort() {
           int temp;
           int i,j;
          
           bool swapped = false;
           
           // loop through all numbers 
           for(i = 0; i < MAX-1; i++) { 
              swapped = false;
            
              // loop through numbers falling ahead 
              for(j = 0; j < MAX-1-i; j++) {
                 printf("     Items compared: [ %d, %d ] ", list[j],list[j+1]);
        
                 // check if next number is lesser than current no
                 //   swap the numbers. 
                 //  (Bubble up the highest number)
              
                 if(list[j] > list[j+1]) {
                    temp = list[j];
                    list[j] = list[j+1];
                    list[j+1] = temp;
        
                    swapped = true;
                    printf(" => swapped [%d, %d]\n",list[j],list[j+1]);
                 } else {
                    printf(" => not swapped\n");
                 }
              
              }
        
              // if no number was swapped that means 
              //   array is sorted now, break the loop. 
              if(!swapped) {
                 break;
              }
              
              printf("Iteration %d#: ",(i+1)); 
              display();
           }
          
        }
        
        void main() {
           printf("Input Array: ");
           display();
           printf("\n");
          
           bubbleSort();
           printf("\nOutput Array: ");
           display();
        }