The bubble sort runs on a quite a simple idea. Pick the first two items, use them to form a "bubble" and then sort the items in the bubble.

Imagine that you have a list a bit like this:

[12, 1, 8, 9, 7, 5]

To sort this using a bubble sort:

  1. take the first two items (i.e. [12, 1]) and sort them – 12 > 1 so we swap them, such that our list now looks like:
[1, 12, 8, 9, 7, 5]
  1. Take the next two items (i.e. [12, 8]) and sort them – 12 is also greater than 8, so we swap them (again) such that our list now looks like:
[1, 8, 12, 9, 7, 5]
  1. We do the next thing for all the other elements – try doing it yourself. The end result should look like:
[1, 8, 9, 7, 5, 12]
  1. We now return to step one (except that we use the list from step 3, in stead of the one we started with). We keep going until the list is sorted. One way of doing this is to keep a boolean variable denoting whether or not we swapped any values. If we didn't swap any values, then the list must be sorted, so we stop.