# Bubble Sort SGSComputing Bubble Sort Algorithm This is

Bubble Sort @SGS_Computing

Bubble Sort Algorithm This is an algorithm that takes random data in a list and sorts it in order by swapping individual items in a list. 21 4 3 30 16 21 22 10 16 30 3414 22 30 413 10 16 22 1 10 4 3 30 16 10 16 25 22 10 31 30 31 22 96 30 25 30 96 25 31 30 96 31 96 1

Bubble Sort Algorithm WHILE List is unsorted DO FOR n items in list DO IF n > n+1 THEN swap items END IF END FOR END WHILE

Bubble Sort Algorithm Arrays (lists) need to be declared at the start of the program Stepper Variables allow us to indicate which iteration we are in, and also point to our current place in the array

Bubble Sort Algorithm This is a call to a user-defined sub-routine. This code will not actually function until we’ve written the procedures!

Bubble Sort Algorithm

Bubble Sort Algorithm

Bubble Sort Algorithm

Bubble Sort Algorithm

Bubble Sort Algorithm

Bubble Sort Algorithm

Bubble Sort Algorithm

Local variables mean that they are only seen within the Scope of our Sort. Data procedure The while loop will keep returning to the start of the array until all the items are sorted The holder variable allows us to swap the two items without clashes in the data Each time an item doesn’t need to be swapped, sorted increments by 1. If sorted = 9 then all data items must be sorted

Bubble Sort Algorithm Entering a list of random numbers allows us to test that the algorithm is working. Try writing out the process in a trace table before running the code to test your logic.

Bubble Sort Algorithm Using the Output. Sort procedure, you can create an animation of how Bubble Sort allows the unsorted section of the array bubble to the top until the whole list is sorted. Towards the end of the algorithm, fewer swaps take place.

- Slides: 15