1.2 Bubble Sort Student

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Bubble Sort

What is bubble sort?


Bubble sort is an algorithm for sorting numbers into ascending order. You could be asked to trace the algorithm and/or compare how efficient it is.

Bubble Sort
What is bubble sort?
Bubble sort is an algorithm for sorting numbers into ascending order. You could be asked to trace the algorithm and/or compare how efficient it is.

Bubble Sort Algorithm


Line 10: If there is only one item in the list, stop Line 20: Make one pass down the list, swapping items as necessary Line 30: If no swaps occurred then stop, otherwise ignore the last item and return to step one In the bubble sort algorithm, the largest number in each pass bubbles to the top, and then is ignored for each successive pass. Even if all the numbers are ordered after a pass, the algorithm does not stop until another pass has been made to ensure there are no swaps to be made. Example Use bubble sort to sort the numbers 6, 4, 8, 3, 7, 4 into ascending order Pass Start Pass 1 List 6 4 4 4 4 4 After Pass 1 Pass 2 4 4 4 4 4 After Pass 2 Pass 3 4 3 3 3 After Pass 3 Pass 4 After Pass 4 3 3 3 3 4 6 6 6 6 6 6 6 3 3 3 3 4 4 4 4 4 4 4 8 8 8 3 3 3 3 3 6 6 6 6 6 6 4 4 4 4 4 3 3 3 8 7 7 7 7 7 7 4 4 4 4 6 6 6 6 6 7 7 7 7 8 4 4 4 4 4 7 7 7 7 7 7 7 7 7 4 4 4 4 4 8 8 8 8 8 8 8 8 8 8 8 8 8 8 Swap 6 and 4 Dont swap 6 and 8 Swap 8 and 3 Swap 8 and 7 Swap 8 and 4 8 is fixed Dont swap 4 and 6 Swap 3 and 6 Dont swap 6 and 7 Swap 7 and 4 7 and 8 are fixed Swap 4 and 3 Dont swap 4 and 6 Swap 6 and 4 6, 7 and 8 are fixed Dont swap 3 and 4 Dont swap 4 and 4 All are fixed Notes

Bubble Sort Algorithm


Line 10: If there is only one item in the list, stop Line 20: Make one pass down the list, swapping items as necessary Line 30: If no swaps occurred then stop, otherwise ignore the last item and return to step one In the bubble sort algorithm, the largest number in each pass bubbles to the top, and then is ignored for each successive pass. Even if all the numbers are ordered after a pass, the algorithm does not stop until another pass has been made to ensure there are no swaps to be made. Example Use bubble sort to sort the numbers 6, 4, 8, 3, 7, 4 into ascending order Pass Start Pass 1 List 6 4 4 4 4 4 After Pass 1 Pass 2 4 4 4 4 4 After Pass 2 Pass 3 4 3 3 3 After Pass 3 Pass 4 After Pass 4 3 3 3 3 4 6 6 6 6 6 6 6 3 3 3 3 4 4 4 4 4 4 4 8 8 8 3 3 3 3 3 6 6 6 6 6 6 4 4 4 4 4 3 3 3 8 7 7 7 7 7 7 4 4 4 4 6 6 6 6 6 7 7 7 7 8 4 4 4 4 4 7 7 7 7 7 7 7 7 7 4 4 4 4 4 8 8 8 8 8 8 8 8 8 8 8 8 8 8 Swap 6 and 4 Dont swap 6 and 8 Swap 8 and 3 Swap 8 and 7 Swap 8 and 4 8 is fixed Dont swap 4 and 6 Swap 3 and 6 Dont swap 6 and 7 Swap 7 and 4 7 and 8 are fixed Swap 4 and 3 Dont swap 4 and 6 Swap 6 and 4 6, 7 and 8 are fixed Dont swap 3 and 4 Dont swap 4 and 4 All are fixed Notes

You might also like