Bubble Sort Algorithm

Bubble Sort Algorithm C++

Bubble Sort Algorithm in C++

Bubble Sort Algorithm in C++

Bubble Sort Algorithm  works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. Bubble sort has worst case and average complexity both O(n2) where n is the number of items being sorted. If n is large then bubble sort is not efficient for sorting large lists.

Bubble Sort Algorithm C++ complexity

  • Worst case performance O(n2)
  • Best case performance O(n)
  • Average case performance O(n2)
  • Worst case space complexity O(1) auxiliary

Bubble Sort Algorithm has worst-case and average complexity both O(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity ofO(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.

Implementation Bubble Sort Algorithm


/*
 * File: bubble_sort.cpp
 * Author: MyCodingLab
 * Code: bubble sort algorithm
 */

#include <cstdlib>
#include <iostream>
using namespace std;

void print_array(int array[], int size) {
 cout<< "buble sort steps: ";
 int j;
 for (j=0; j<size;j++)
 cout <<" "<< array[j];
 cout << endl;
}//end of print_array

void bubble_sort(int arr[], int size) {
 bool not_sorted = true;
 int j=1,tmp; 

 while (not_sorted)  {
 not_sorted = false;
 j++;
 for (int i = 0; i < size - j; i++) {
 if (arr[i] > arr[i + 1]) {
 tmp = arr[i];
 arr[i] = arr[i + 1];
 arr[i + 1] = tmp;
 not_sorted = true;

    }//end of if
   print_array(arr,5);
  }//end of for loop
 }//end of while loop
}//end of bubble_sort

int main() {
 int array[5]= {5,4,3,2,1};
 print_array(array,5);
 bubble_sort(array,5);
 return 0;
}//end of main

Program output

bubble sort steps: 5 4 3 2 1
bubble sort steps: 4 5 3 2 1
bubble sort steps: 4 3 5 2 1
bubble sort steps: 4 3 2 5 1
bubble sort steps: 4 3 2 1 5
bubble sort steps: 3 4 2 1 5
bubble sort steps: 3 2 4 1 5
bubble sort steps: 3 2 1 4 5
bubble sort steps: 2 3 1 4 5
bubble sort steps: 2 1 3 4 5
bubble sort steps: 1 2 3 4 5

Step-by-step example Bubble Sort Algorithm

Let us take the array of numbers “5 1 4 2 8″, and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.

First Pass:
( 5 1 4 2 8 ) \to ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) \to ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) \to ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) \to ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) \to ( 1 4 2 5 8 )
( 1 4 2 5 8 ) \to ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )

Reference: Bubble Sort

You must be logged in to post a comment.

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com
%d bloggers like this: