|
Bubble Sort Algorithm
Worked Example
Sort The Numbers Into Ascending Order
arranged from left to right
We start working from left to right
and compare each pair of numbers as we reach them - 1st & 2rd, 2rd & 3rd, 3rd & 4th then 4th & 5th
If the number on the left is larger then we move it to the right
and the number that was on the right we move to the left
If the number on the left is smaller then we leave that pair of numbers as they are
| 7 |
4 |
9 |
6 |
1 |
| 4 |
7 |
9 |
6 |
1 |
| 4 |
7 |
6 |
9 |
1 |
| 4 |
7 |
6 |
1 |
9 |
End of first pass - the largest number is at the right hand side
Again, work from left to right
| 4 |
7 |
6 |
1 |
9 |
| 4 |
6 |
7 |
1 |
9 |
| 4 |
6 |
1 |
7 |
9 |
End of second pass - the largest two numbers are at the right hand side
Now again, work from left to right
End of third pass - the largest three numbers are at the right hand side
Last time, work from left to right
End of fourth pass
the numbers are in order
|