bubble sort · 2013. 11. 6. · bubble sort with thanks to bill leahy, gatech for original slide...

96
Bubble Sort With thanks to Bill Leahy, GaTech for original slide deck

Upload: others

Post on 27-Jan-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

  • Bubble Sort

    With thanks to Bill Leahy, GaTech for original slide deck

  • Sorting

    • Sorting takes an unordered collection and makes it an ordered one.

    512354277 101

    0 1 2 3 4 5

    5 12 35 42 77 101

    0 1 2 3 4 5

  • Sorting

    • Lots of algorithms for sorting– Selection sort (video lecture and textbook)– Insertion sort (textbook)– Bubble sort– Merge sort – Radix sort– ...

  • "Bubbling Up" the Largest Element

    • Traverse a collection of elements (array)– Move from the front to the end– “Bubble” the largest value to the end using

    pair-wise comparisons and swapping

    512354277 101

    0 1 2 3 4 5

  • "Bubbling Up" the Largest Element

    • Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using

    pair-wise comparisons and swapping

    512354277 101Swap42 77

    0 1 2 3 4 5

  • "Bubbling Up" the Largest Element

    • Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using

    pair-wise comparisons and swapping

    512357742 101Swap35 77

    0 1 2 3 4 5

  • "Bubbling Up" the Largest Element

    • Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using

    pair-wise comparisons and swapping

    512773542 101Swap12 77

    0 1 2 3 4 5

  • "Bubbling Up" the Largest Element

    • Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using

    pair-wise comparisons and swapping

    577123542 101

    No need to swap

    0 1 2 3 4 5

  • "Bubbling Up" the Largest Element

    • Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using

    pair-wise comparisons and swapping

    577123542 101Swap5 101

    0 1 2 3 4 5

  • "Bubbling Up" the Largest Element

    • Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using

    pair-wise comparisons and swapping

    77123542 5 101

    Largest value correctly placed

    0 1 2 3 4 5

  • The “Bubble Up” Algorithm

    Given some array, a

    for(int i = 0; i < a.length-1; i++) { if(a[i] > a[i+1]) {

    int temp = a[i];

    a[i] = a[i+1];

    a[i+1] = temp; }}

  • Items of Interest

    • Notice that only the largest value is correctly placed

    • All other values are still out of order• So we need to repeat this process

    77123542 5 101

    Largest value correctly placed

    0 1 2 3 4 5

  • Repeat “Bubble Up” How Many Times?

    • If we have N elements…

    • And if each time we bubble an element, we place it in its correct location…

    • Then we repeat the “bubble up” process N – 1 times.

    • This guarantees we’ll correctly place all N elements.

  • “Bubbling” All the Elements

    77123542 5 101

    5421235 770 1 2 3 4 5

    101

    42 5 3512 770 1 2 3 4 5

    101

    42 35 512 770 1 2 3 4 5

    101

    42 35 12 5 770 1 2 3 4 5

    101

    N -

    1

    0 1 2 3 4 5

  • Reducing the Number of Comparisons

    12 35 42 77 1010 1 2 3 4 5

    5

    77123542 50 1 2 3 4 5

    101

    5421235 770 1 2 3 4 5

    101

    42 5 3512 770 1 2 3 4 5

    101

    42 35 512 770 1 2 3 4 5

    101

  • Reducing the Number of Comparisons• On the Nth “bubble up”, we only need to

    do MAX-N comparisons.

    • For example:– This is the 4th “bubble up”– MAX is 6 (total number of elements in array)– Thus we have 2 comparisons to do

    42 5 3512 770 1 2 3 4 5

    101

  • Putting It All Together

  • public static void Bubblesort(int[] a){ int to_do = a.length-1;

    while (to_do != 0) { for(int i = 0; i < to_do; i++) { if(a[i] > a[i+1]) { int temp = a[i];

    a[i] = a[i+1];

    a[i+1] = temp; } } to_do = to_do - 1} //end method

    Inne

    r lo

    op

    Out

    er lo

    op

  • Already Sorted Collections?

    • What if the collection was already sorted?• What if only a few elements were out of place and

    after a couple of “bubble ups,” the collection was sorted?

    • We want to be able to detect this and “stop early”!

    42 35 12 5 770 1 2 3 4 5

    101

  • Using a Boolean “Flag”

    • We can use a boolean variable to determine if any swapping occurred during the “bubble up.”

    • If no swapping occurred, then we know that the collection is already sorted!

    • This boolean “flag” needs to be reset after each “bubble up.”

  • public static void Bubblesort(int[] a){ int to_do = a.length-1; boolean did_swap = true;

    while (to_do != 0 && did_swap) { did_swap = false; for(int i = 0; i < to_do; i++) { if(a[i] > a[i+1]) { did_swap = true; int temp = a[i];

    a[i] = a[i+1];

    a[i+1] = temp; } } to_do = to_do - 1} //end method

  • An Animated Example

    674523 14 6 3398 42

    to_do

    i

    7

    0

    did_swap false

    0 1 2 3 4 5 6 7

  • An Animated Example

    674523 14 6 3398 42

    to_do

    i

    7

    0

    Swap

    did_swap false

    0 1 2 3 4 5 6 7

  • An Animated Example

    674598 14 6 3323 42

    to_do

    i

    7

    1

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    674598 14 6 3323 42

    to_do

    i

    7

    1

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    674598 14 6 3323 42

    to_do

    i

    7

    1

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    679845 14 6 3323 42

    to_do

    i

    7

    1

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    679845 14 6 3323 42

    to_do

    i

    7

    2

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    679845 14 6 3323 42

    to_do

    i

    7

    2

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    671445 98 6 3323 42

    to_do

    i

    7

    2

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    671445 98 6 3323 42

    to_do

    i

    7

    3

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    671445 98 6 3323 42

    to_do

    i

    7

    3

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    671445 6 98 3323 42

    to_do

    i

    7

    3

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    671445 6 98 3323 42

    to_do

    i

    7

    4

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    671445 6 98 3323 42

    to_do

    i

    7

    4

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    981445 6 67 3323 42

    to_do

    i

    7

    4

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    981445 6 67 3323 42

    to_do

    i

    7

    5

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    981445 6 67 3323 42

    to_do

    i

    7

    5

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    331445 6 67 9823 42

    to_do

    i

    7

    5

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    331445 6 67 9823 42

    to_do

    i

    7

    6

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    331445 6 67 9823 42

    to_do

    i

    7

    6

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • An Animated Example

    331445 6 67 4223 98

    to_do

    i

    7

    6

    Swap

    did_swap true

    0 1 2 3 4 5 6 7

  • After First Pass of Outer Loop

    331445 6 67 4223 98

    to_do

    i

    7

    7 Finished first “Bubble Up”

    did_swap true

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    331445 6 67 4223 98

    to_do

    i

    6

    0

    did_swap false

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    331445 6 67 4223 98

    to_do

    i

    6

    0

    did_swap false

    No Swap

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    331445 6 67 4223 98

    to_do

    i

    6

    1

    did_swap false

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    331445 6 67 4223 98

    to_do

    i

    6

    1

    did_swap false

    Swap

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    334514 6 67 4223 98

    to_do

    i

    6

    1

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    334514 6 67 4223 98

    to_do

    i

    6

    2

    did_swap true

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    334514 6 67 4223 98

    to_do

    i

    6

    2

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    33614 45 67 4223 98

    0 1 2 3 4 5 6 7

    to_do

    i

    6

    2

    did_swap true

    Swap

  • The Second “Bubble Up”

    33614 45 67 4223 98

    to_do

    i

    6

    3

    did_swap true

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    33614 45 67 4223 98

    to_do

    i

    6

    3

    did_swap true

    No Swap

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    33614 45 67 4223 98

    to_do

    i

    6

    4

    did_swap true

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    33614 45 67 4223 98

    to_do

    i

    6

    4

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    67614 45 33 4223 98

    to_do

    i

    6

    4

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    67614 45 33 4223 98

    to_do

    i

    6

    5

    did_swap true

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    67614 45 33 4223 98

    to_do

    i

    6

    5

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Second “Bubble Up”

    42614 45 33 6723 98

    to_do

    i

    6

    5

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • After Second Pass of Outer Loop

    42614 45 33 6723 98

    to_do

    i

    6

    6

    did_swap true

    Finished second “Bubble Up”

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42614 45 33 6723 98

    to_do

    i

    5

    0

    did_swap false

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42614 45 33 6723 98

    to_do

    i

    5

    0

    did_swap false

    Swap

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42623 45 33 6714 98

    to_do

    i

    5

    0

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42623 45 33 6714 98

    to_do

    i

    5

    1

    did_swap true

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42623 45 33 6714 98

    to_do

    i

    5

    1

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42236 45 33 6714 98

    to_do

    i

    5

    1

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42236 45 33 6714 98

    to_do

    i

    5

    2

    did_swap true

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42236 45 33 6714 98

    to_do

    i

    5

    2

    did_swap true

    No Swap

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42236 45 33 6714 98

    to_do

    i

    5

    3

    did_swap true

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42236 45 33 6714 98

    to_do

    i

    5

    3

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42236 33 45 6714 98

    to_do

    i

    5

    3

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42236 33 45 6714 98

    to_do

    i

    5

    4

    did_swap true

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    42236 33 45 6714 98

    to_do

    i

    5

    4

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Third “Bubble Up”

    45236 33 42 6714 98

    to_do

    i

    5

    4

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • After Third Pass of Outer Loop

    45236 33 42 6714 98

    to_do

    i

    5

    5

    did_swap true

    Finished third “Bubble Up”

    0 1 2 3 4 5 6 7

  • The Fourth “Bubble Up”

    45236 33 42 6714 98

    to_do

    i

    4

    0

    did_swap false

    0 1 2 3 4 5 6 7

  • The Fourth “Bubble Up”

    45236 33 42 6714 98

    to_do

    i

    4

    0

    did_swap false

    Swap

    0 1 2 3 4 5 6 7

  • The Fourth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    4

    0

    did_swap true

    Swap

    0 1 2 3 4 5 6 7

  • The Fourth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    4

    1

    did_swap true

    0 1 2 3 4 5 6 7

  • The Fourth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    4

    1

    did_swap true

    No Swap

    0 1 2 3 4 5 6 7

  • The Fourth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    4

    2

    did_swap true

    0 1 2 3 4 5 6 7

  • The Fourth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    4

    2

    did_swap true

    No Swap

    0 1 2 3 4 5 6 7

  • The Fourth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    4

    3

    did_swap true

    0 1 2 3 4 5 6 7

  • The Fourth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    4

    3

    did_swap true

    No Swap

    0 1 2 3 4 5 6 7

  • After Fourth Pass of Outer Loop

    452314 33 42 676 98

    to_do

    i

    4

    4

    did_swap true

    Finished fourth “Bubble Up”

    0 1 2 3 4 5 6 7

  • The Fifth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    3

    0

    did_swap false

    0 1 2 3 4 5 6 7

  • The Fifth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    3

    0

    did_swap false

    No Swap

    0 1 2 3 4 5 6 7

  • The Fifth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    3

    1

    did_swap false

    0 1 2 3 4 5 6 7

  • The Fifth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    3

    1

    did_swap false

    No Swap

    0 1 2 3 4 5 6 7

  • The Fifth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    3

    2

    did_swap false

    0 1 2 3 4 5 6 7

  • The Fifth “Bubble Up”

    452314 33 42 676 98

    to_do

    i

    3

    2

    did_swap false

    No Swap

    0 1 2 3 4 5 6 7

  • After Fifth Pass of Outer Loop

    452314 33 42 676 98

    to_do

    i

    3

    3

    did_swap false

    Finished fifth “Bubble Up”

    0 1 2 3 4 5 6 7

  • Finished “Early”

    452314 33 42 676 98

    to_do

    i

    3

    3

    did_swap false We didn’t do any swapping,so all of the other elementsmust be correctly placed.

    We can “skip” the last twopasses of the outer loop.

    0 1 2 3 4 5 6 7

  • Summary

    • “Bubble Up” algorithm will move largest value to its correct location (to the right/end of array)

    • Repeat “Bubble Up” until all elements are correctly placed:– Maximum of N-1 times– Can finish early if no swapping occurs

    • We reduce the number of elements we compare each time one is correctly placed

  • Truth in CS Act

    • NOBODY EVER USES BUBBLE SORT

    • NOBODY

    • NOT EVER

    • BECAUSE IT IS EXTREMELY INEFFICIENT

    LB

  • Questions?

    Bubble Sort Merge SortSortingSlide 3"Bubbling Up" the Largest ElementSlide 5Slide 6Slide 7Slide 8Slide 9Slide 10The “Bubble Up” AlgorithmItems of InterestRepeat “Bubble Up” How Many Times?“Bubbling” All the ElementsReducing the Number of ComparisonsSlide 16Putting It All TogetherSlide 18Already Sorted Collections?Using a Boolean “Flag”Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42After First Pass of Outer LoopThe Second “Bubble Up”Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59After Second Pass of Outer LoopThe Third “Bubble Up”Slide 62Slide 63Slide 64Slide 65Slide 66Slide 67Slide 68Slide 69Slide 70Slide 71Slide 72Slide 73Slide 74After Third Pass of Outer LoopThe Fourth “Bubble Up”Slide 77Slide 78Slide 79Slide 80Slide 81Slide 82Slide 83Slide 84After Fourth Pass of Outer LoopThe Fifth “Bubble Up”Slide 87Slide 88Slide 89Slide 90Slide 91After Fifth Pass of Outer LoopSlide 93SummaryTruth in CS ActQuestions?