1.2 Bubble Sort student.docx

download 1.2 Bubble Sort student.docx

of 2

Transcript of 1.2 Bubble Sort student.docx

  • 7/29/2019 1.2 Bubble Sort student.docx

    1/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 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 List Notes

    Start 6 4 8 3 7 4

    Pass 1 4 6 8 3 7 4 Swap 6 and 4

    4 6 8 3 7 4 Dont swap 6 and 8

    4 6 3 8 7 4 Swap 8 and 3

    4 6 3 7 8 4 Swap 8 and 7

    4 6 3 7 4 8 Swap 8 and 4

    After Pass 1 4 6 3 7 4 8 8 is fixed

    Pass 2 4 6 3 7 4 8 Dont swap 4 and 6

    4 3 6 7 4 8 Swap 3 and 6

    4 3 6 7 4 8 Dont swap 6 and 7

    4 3 6 4 7 8 Swap 7 and 4

    After Pass 2 4 3 6 4 7 8 7 and 8 are fixed

    Pass 3 3 4 6 4 7 8 Swap 4 and 3

    3 4 6 4 7 8 Dont swap 4 and 6

    3 4 4 6 7 8 Swap 6 and 4

    After Pass 3 3 4 4 6 7 8 6, 7 and 8 are fixed

    Pass 4 3 4 4 6 7 8 Dont swap 3 and 4

    3 4 4 6 7 8 Dont swap 4 and 4

    After Pass 4 3 4 4 6 7 8 All are fixed

    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 List Notes

    Start 6 4 8 3 7 4

    Pass 1 4 6 8 3 7 4 Swap 6 and 4

    4 6 8 3 7 4 Dont swap 6 and 8

    4 6 3 8 7 4 Swap 8 and 3

    4 6 3 7 8 4 Swap 8 and 7

    4 6 3 7 4 8 Swap 8 and 4

    After Pass 1 4 6 3 7 4 8 8 is fixed

    Pass 2 4 6 3 7 4 8 Dont swap 4 and 6

    4 3 6 7 4 8 Swap 3 and 6

    4 3 6 7 4 8 Dont swap 6 and 7

    4 3 6 4 7 8 Swap 7 and 4

    After Pass 2 4 3 6 4 7 8 7 and 8 are fixed

    Pass 3 3 4 6 4 7 8 Swap 4 and 3

    3 4 6 4 7 8 Dont swap 4 and 6

    3 4 4 6 7 8 Swap 6 and 4

    After Pass 3 3 4 4 6 7 8 6, 7 and 8 are fixed

    Pass 4 3 4 4 6 7 8 Dont swap 3 and 4

    3 4 4 6 7 8 Dont swap 4 and 4

    After Pass 4 3 4 4 6 7 8 All are fixed

  • 7/29/2019 1.2 Bubble Sort student.docx

    2/2