Wednesday, June 8, 2011

Bubble Sort in C++

Leave a Comment
What is bubble sort algorithm? How to implement in C++?
  • Bubble sort is a simple sorting algorithm.
  • Best case time complexity is O(n) when the list is already sorted.
  • Average and Worst case complexity is O(n*n). So it is not recommended for large input sets.
  • Bubble sort involves these simple steps.
    • Compare adjacent items and swap them if they are not in the correct order.
    • Perform the above step until no exchanges are required,

EXAMPLE:- A sample implementation of bubble sort using C++

#include <iostream>
using namespace std;

static int count = 0;
const int INPUT_SIZE = 10;

void print(int *input)
{
for ( int i = 0; i < INPUT_SIZE; i++ )
cout << input[i] << " ";
cout << endl;
}

void bubblesort(int* input)
{
count++;
int exchanges = 0;
for ( int i = 0; i < INPUT_SIZE; i++ )
{
if ( input[i] > input[i+1] )
{
int tmp = input[i];
input[i] = input[i+1];
input[i+1] = tmp;
exchanges++;
}
}

if ( exchanges == 0 ) return;
cout << "Parse " << count << ":";
print(input);
bubblesort(input);
}

int main()
{
int input[INPUT_SIZE] = {500, 700, 800, 100, 300, 200, 900, 400, 1000, 600};
cout << "Input: ";
print(input);
bubblesort(input);
cout << "Output: ";
print(input);
return 0;
}

OUTPUT:-
Input: 500 700 800 100 300 200 900 400 1000 600
Parse 1:500 700 100 300 200 800 400 900 600 1000
Parse 2:500 100 300 200 700 400 800 600 900 1000
Parse 3:100 300 200 500 400 700 600 800 900 1000
Parse 4:100 200 300 400 500 600 700 800 900 1000
Output: 100 200 300 400 500 600 700 800 900 1000
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment