Bubble Sort Algorithm

The Bubble Sort algorithm is one the simplest sorting algorithm to implement. It works by continually looping through pairs in a list and comparing each pair. At each pair, it will perform a swap if the pair happens to be in the wrong order. The order will either be ascending or descending depending on what the programmer wants. This happens until no more swaps are needed. This algorithm apparently gets the name “bubble-sort” because you can imagine the smaller elements bubble to the top of the list. Checkout my bubble sort implementation below. When ran, it will give a good visual reference for the algorithm. It sorts in ascending order, complexity O(n^2). This algorithm approaches O(n) if the elements are already almost in order. Note that is the not the most efficient algorithm for large data sets.

#include <iostream>

using namespace std;

int main()
int array[7];
int i, j;

for(i = 0; i <= 6; i++){
cout << "Enter unsorted numbers: " << endl;
cin >> array[i];
for(i = 0; i <= 5; i++){ //loop through pair of integers
for(j = i+1; j <= 6; j++){
int temp;
if(array[i] > array[j])
temp = array[i]; //swapping mechanisms
array[i] = array[j];
array[j] = temp;
for(i = 0; i <= 6; i++){
cout << array[i]; //sorted output
return 0;


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s