Bubble sort in C/C++

// Bubble Sort has a performance of O(n^2) and is a comparison sort

void doBubbleSort(){

int a[4];

a[0] = 1;

a[1] = 0;

a[2] = 10;

a[3] = 6;

int len = sizeof(a)/sizeof(a[0]);

// The actual Bubble Sort Algorithm

bool bSwaps = true;

while ( bSwaps ){

bSwaps = false;

for( int i =1; i < len; i++ ){

if ( a[i] < a[i-1] ){

int temp = a[i-1];

a[i-1] = a[i];

a[i] = temp;

bSwaps = true;

}

}

}

// Print out the array now sorted

for ( int i=0; i < len; i++){

std::cout << a[i] << ” “;

}

}

Advertisements
This entry was posted in Development and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s