Presentation on theme: "Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade)"— Presentation transcript:
Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade) You can “bubble” from bottom to top, right to left, top to bottom or left to right as long as you “bubble” in the same direction. Assuming you are “bubbling” from right to left: Step 1 Compare the last two numbers on the extreme right. If the smaller number is on the right, swap the two numbers and reorder the list, if not, leave them. Step 2 Move one step back in the list (to the left) and compare the two numbers. If the smaller is on the right swap the two numbers and reorder the list, if not, leave them. Step 3 Repeat Step 2 until the two numbers on the extreme left have been compared, then return to Step 1. Step 4 Repeat Step 3 until all the numbers are in order (i.e. no more swaps are performed in a pass)
Sort the following numbers into ascending order: 27 15 2 38 16 1 27152381611 13812115271 This is the result of the first pass – the smallest number is now in the right position – record this! 1 152 1 27 15 2 38 16 No swap 215272 This is the result of the second pass - the 2 is now in the correct position – record this! 1 2 27 15 16 38 12 1516382715 This is the result of the third pass - the 15 is now in the correct position – record this! 1 2 15 27 16 38 12 271638 No swap 1627 This is the result of the fourth pass - the 16 is now in the correct position – record this! 1 2 15 16 27 38 1215162738 The final pass yields no change but you should still record this! 1 2 15 16 27 38 STOP 38163816
Now try sorting this list: Bubble from right to left 253617941375711 925361711413757 911253617374157 911172536374157 STOP Now try sorting the list: Bubble from left to right 253617941375711 251793637411157 179253637114157 917253611374157 917251136374157 917112536374157 911172536374157 STOP
Your consent to our cookies if you continue to use this website.