# Bubble Sort¶

We need to see a another simple sorting scheme to round out our introduction to searching and sorting. Let’s look at one of the easiest to understand, the Bubble Sort scheme.

## Bubble sort thinking¶

First, we will think this through. Pretend that you have a deck or numbered cards, no suits, just numbered, and you want to sort them. What we are going to do is simple. The cards are in a pile numbers up. We will slide the top card off so we can see the top two cards. If they are in the right order (say we are sorting low to high), we will place the first card face down on the left and slide the second card over exposing the third card. If the second and third card are not in the right order, we will swap them, moving the third card to the second position, and the second card to the third position. Then we will place the new second card face down on top of the first card. We continue this process until we reach the end of the deck, swapping each time we find cards out of order.

Bubble sort gets is name since the biggest card in the deck sort of “bubbles” to the bottom of the deck on this first pass.

Once we finish the first pass, we repeat the process for a second pass. Guess what, the second biggest card in the deck “bubbles” to the next to the bottom place in the deck on this pass. We do this again and again, until on the last pass nothing moves and the entire deck is sorted.

Bubble sort can be optimized a bit by realizing that you do not need to sweep all the way to the end of the deck on subsequent passes since the last ones will never swap due to out of order compares. The code for an array bubble sort looks like this:

for(int i=0;i<num_cards-1;i++)
for(j=0,j<num_cards-i-1;j++){
if(card[j]>card[j+1])
// swap cards
temp = card[j]
card[j] = card[j+1]
card[j+1] = temp


Once you learn this code, it is hard to forget. It is not that efficient, but it does work pretty well for small sorting problems.

## Optimizing Bubble Sort¶

We can make another optimization by tracking if we have made any swaps and stopping when we have stopped. Let’s add a flag variable to track that. And we will write this as a function to sort data in an array, not cards.

void BubbleSort(int data[], int size) {
int i,j,flag;
int temp;

flag = 1;   //swaps must be done to start
for(i = 0; (i< size-1) && flag; i++) {
flag = 0;       // say no swaps seen yet
for (j=0;j<size-i-i;j++){
if(data[j+1]>num[j]) {  // swap
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
flag = 1;
}
}
}
}


## Testing Bubble Sort¶

Here is a simple program that you can run to see Bubble Sort (unoptimized) in action:

#include <iostream>
using namespace std;

int data[] = {5,2,7,4,1,9,6,8,0,4};

void BubbleSort(int data[], int size) {
int temp,i,j,k;
for(i=0;i<size-1;i++) {
for(j=0;j<size-1-i;j++)
if(data[j] > data[j+1]) {
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
for(k=0;k<size;k++)
cout << data[k] << " ";
cout << endl;
}
}

int main(void) {
BubbleSort(data,10);
}


This is what you should see:

./sort
2 5 4 1 7 6 8 0 4 9
2 4 1 5 6 7 0 4 8 9
2 1 4 5 6 0 4 7 8 9
1 2 4 5 0 4 6 7 8 9
1 2 4 0 4 5 6 7 8 9
1 2 0 4 4 5 6 7 8 9
1 0 2 4 4 5 6 7 8 9
0 1 2 4 4 5 6 7 8 9
0 1 2 4 4 5 6 7 8 9


Looks like it works.