14085 Bubble Merge

download 14085 Bubble Merge

of 151

Transcript of 14085 Bubble Merge

  • 8/6/2019 14085 Bubble Merge

    1/151

    Bubble Sort

    Merge Sort

  • 8/6/2019 14085 Bubble Merge

    2/151

    Bubble Sort

  • 8/6/2019 14085 Bubble Merge

    3/151

    Sorting

    Sorting takes an unordered collection and

    makes it an ordered one.

    512354277 101

    1 2 3 4 5 6

    5 12 35 42 77 101

    1 2 3 4 5 6

  • 8/6/2019 14085 Bubble Merge

    4/151

    "Bubbling Up" the Largest Element

    Traverse a collection of elements

    Move from the front to the end

    Bubble the largest value to the end usingpair-wise comparisons and swapping

    512354277 101

    1 2 3 4 5 6

  • 8/6/2019 14085 Bubble Merge

    5/151

    "Bubbling Up" the Largest Element

    Traverse a collection of elements

    Move from the front to the end

    Bubble the largest value to the end usingpair-wise comparisons and swapping

    512354277 101

    1 2 3 4 5 6

    Swap

    42 77

  • 8/6/2019 14085 Bubble Merge

    6/151

    "Bubbling Up" the Largest Element

    Traverse a collection of elements

    Move from the front to the end

    Bubble the largest value to the end usingpair-wise comparisons and swapping

    512357742 101

    1 2 3 4 5 6

    Swap

    35 77

  • 8/6/2019 14085 Bubble Merge

    7/151

    "Bubbling Up" the Largest Element

    Traverse a collection of elements

    Move from the front to the end

    Bubble the largest value to the end usingpair-wise comparisons and swapping

    512773542 101

    1 2 3 4 5 6

    Swap12 77

  • 8/6/2019 14085 Bubble Merge

    8/151

    "Bubbling Up" the Largest Element

    Traverse a collection of elements

    Move from the front to the end

    Bubble the largest value to the end usingpair-wise comparisons and swapping

    577123542 101

    1 2 3 4 5 6

    No need to swap

  • 8/6/2019 14085 Bubble Merge

    9/151

    "Bubbling Up" the Largest Element

    Traverse a collection of elements

    Move from the front to the end

    Bubble the largest value to the end usingpair-wise comparisons and swapping

    577123542 101

    1 2 3 4 5 6

    Swap5 101

  • 8/6/2019 14085 Bubble Merge

    10/151

    "Bubbling Up" the Largest Element

    Traverse a collection of elements

    Move from the front to the end

    Bubble the largest value to the end usingpair-wise comparisons and swapping

    77123542 5

    1 2 3 4 5 6

    101

    Largest value correctly placed

  • 8/6/2019 14085 Bubble Merge

    11/151

    The Bubble Up Algorithm

    index A[index + 1]) then

    Swap(A[index], A[index + 1])

    endif

    index

  • 8/6/2019 14085 Bubble Merge

    12/151

    No, Swap isnt built in.

    Procedure Swap(a, b isoftype in/outNum)

    t isoftypeNum

    t

  • 8/6/2019 14085 Bubble Merge

    13/151

    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

    1 2 3 4 5 6

    101

    Largest value correctly placed

  • 8/6/2019 14085 Bubble Merge

    14/151

    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 upprocess N 1 times.

    This guarantees well correctlyplace all N elements.

  • 8/6/2019 14085 Bubble Merge

    15/151

    Bubbling All the Elements

    77123542 5

    1 2 3 4 5 6

    101

    5421235 77

    1 2 3 4 5 6

    101

    4253512 77

    1 2 3 4 5 6

    101

    4235512 771 2 3 4 5 6101

    4235125 77

    1 2 3 4 5 6

    101

    N

    -1

  • 8/6/2019 14085 Bubble Merge

    16/151

    Reducing the Number of Comparisons

    12354277 101

    1 2 3 4 5 6

    5

    77123542 5

    1 2 3 4 5 6

    101

    5421235 77

    1 2 3 4 5 6

    101

    4253512 77

    1 2 3 4 5 6

    101

    4235512 77

    1 2 3 4 5 6

    101

  • 8/6/2019 14085 Bubble Merge

    17/151

    Reducing the Number of Comparisons

    On the Nth bubble up, we only need todo MAX-N comparisons.

    For example:

    This is the 4th bubble up

    MAX is 6

    Thus we have 2 comparisons to do

    4253512 77

    1 2 3 4 5 6

    101

  • 8/6/2019 14085 Bubble Merge

    18/151

    Putting It All Together

  • 8/6/2019 14085 Bubble Merge

    19/151

    N is // Size of Array

    Arr_Type definesa Array[1..N] of Num

    Procedure Swap(n1, n2 isoftype in/out Num)temp isoftype Num

    temp

  • 8/6/2019 14085 Bubble Merge

    20/151

    procedure Bubblesort(A isoftype in/out Arr_Type)

    to_do, index isoftype Num

    to_do A[index + 1]) then

    Swap(A[index], A[index + 1])

    endif

    index

  • 8/6/2019 14085 Bubble Merge

    21/151

    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!

    4235125 77

    1 2 3 4 5 6

    101

  • 8/6/2019 14085 Bubble Merge

    22/151

    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 eachbubble up.

  • 8/6/2019 14085 Bubble Merge

    23/151

    did_swap isoftype Boolean

    did_swap

  • 8/6/2019 14085 Bubble Merge

    24/151

  • 8/6/2019 14085 Bubble Merge

    25/151

    An Animated Example

    674523 14 6 3398 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    1

    N 8 did_swap false

  • 8/6/2019 14085 Bubble Merge

    26/151

    An Animated Example

    674523 14 6 3398 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    1

    N 8

    Swap

    did_swap false

  • 8/6/2019 14085 Bubble Merge

    27/151

    An Animated Example

    674598 14 6 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    1

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    28/151

    An Animated Example

    674598 14 6 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    2

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    29/151

    An Animated Example

    674598 14 6 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    2

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    30/151

    An Animated Example

    679845 14 6 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    2

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    31/151

    An Animated Example

    679845 14 6 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    3

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    32/151

    An Animated Example

    679845 14 6 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    3

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    33/151

    An Animated Example

    671445 98 6 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    3

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    34/151

    An Animated Example

    671445 98 6 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    4

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    35/151

    An Animated Example

    671445 98 6 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    4

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    36/151

    An Animated Example

    671445 6 98 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    4

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    37/151

    An Animated Example

    671445 6 98 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    5

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    38/151

    An Animated Example

    671445 6 98 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    5

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    39/151

    An Animated Example

    981445 6 67 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    5

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    40/151

    An Animated Example

    981445 6 67 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    6

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    41/151

    An Animated Example

    981445 6 67 3323 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    6

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    42/151

    An Animated Example

    331445 6 67 9823 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    6

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    43/151

    An Animated Example

    331445 6 67 9823 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    7

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    44/151

    An Animated Example

    331445 6 67 9823 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    7

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    45/151

    An Animated Example

    331445 6 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    7

    N 8

    Swap

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    46/151

    After First Pass of Outer Loop

    331445 6 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    8

    N 8

    Finished first Bubble Up

    did_swap true

  • 8/6/2019 14085 Bubble Merge

    47/151

    The Second Bubble Up

    331445 6 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    1

    N 8 did_swap false

  • 8/6/2019 14085 Bubble Merge

    48/151

    The Second Bubble Up

    331445 6 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    1

    N 8 did_swap false

    No Swap

  • 8/6/2019 14085 Bubble Merge

    49/151

    The Second Bubble Up

    331445 6 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    2

    N 8 did_swap false

  • 8/6/2019 14085 Bubble Merge

    50/151

    The Second Bubble Up

    331445 6 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    2

    N 8 did_swap false

    Swap

  • 8/6/2019 14085 Bubble Merge

    51/151

    The Second Bubble Up

    334514 6 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    2

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    52/151

    The Second Bubble Up

    334514 6 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    3

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    53/151

    The Second Bubble Up

    334514 6 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    3

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    54/151

    The Second Bubble Up

    33614 45 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    3

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    55/151

    The Second Bubble Up

    33614 45 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    4

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    56/151

    The Second Bubble Up

    33614 45 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    4

    N 8 did_swap true

    No Swap

  • 8/6/2019 14085 Bubble Merge

    57/151

    The Second Bubble Up

    33614 45 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    5

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    58/151

    The Second Bubble Up

    33614 45 67 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    5

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    59/151

    The Second Bubble Up

    67614 45 33 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    5

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    60/151

    The Second Bubble Up

    67614 45 33 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    6

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    61/151

    The Second Bubble Up

    67614 45 33 4223 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    6

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    62/151

  • 8/6/2019 14085 Bubble Merge

    63/151

    After Second Pass of Outer Loop

    42614 45 33 6723 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    7

    N 8 did_swap true

    Finished second Bubble Up

  • 8/6/2019 14085 Bubble Merge

    64/151

    The Third Bubble Up

    42614 45 33 6723 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    1

    N 8 did_swap false

  • 8/6/2019 14085 Bubble Merge

    65/151

    The Third Bubble Up

    42614 45 33 6723 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    1

    N 8 did_swap false

    Swap

  • 8/6/2019 14085 Bubble Merge

    66/151

    The Third Bubble Up

    42623 45 33 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    1

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    67/151

    The Third Bubble Up

    42623 45 33 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    2

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    68/151

    The Third Bubble Up

    42623 45 33 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    2

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    69/151

    The Third Bubble Up

    42236 45 33 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    2

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    70/151

    The Third Bubble Up

    42236 45 33 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    3

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    71/151

    The Third Bubble Up

    42236 45 33 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    3

    N 8 did_swap true

    No Swap

  • 8/6/2019 14085 Bubble Merge

    72/151

    The Third Bubble Up

    42236 45 33 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    4

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    73/151

    The Third Bubble Up

    42236 45 33 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    4

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    74/151

    The Third Bubble Up

    42236 33 45 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    4

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    75/151

    The Third Bubble Up

    42236 33 45 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    5

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    76/151

    The Third Bubble Up

    42236 33 45 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    5

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    77/151

    The Third Bubble Up

    45236 33 42 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    5

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    78/151

    After Third Pass of Outer Loop

    45236 33 42 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    5

    6

    N 8 did_swap true

    Finished third Bubble Up

  • 8/6/2019 14085 Bubble Merge

    79/151

    The Fourth Bubble Up

    45236 33 42 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    4

    1

    N 8 did_swap false

  • 8/6/2019 14085 Bubble Merge

    80/151

    The Fourth Bubble Up

    45236 33 42 6714 98

    1 2 3 4 5 6 7 8

    to_do

    index

    4

    1

    N 8 did_swap false

    Swap

  • 8/6/2019 14085 Bubble Merge

    81/151

    The Fourth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    4

    1

    N 8 did_swap true

    Swap

  • 8/6/2019 14085 Bubble Merge

    82/151

    The Fourth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    4

    2

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    83/151

    The Fourth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    4

    2

    N 8 did_swap true

    No Swap

  • 8/6/2019 14085 Bubble Merge

    84/151

  • 8/6/2019 14085 Bubble Merge

    85/151

    The Fourth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    4

    3

    N 8 did_swap true

    No Swap

  • 8/6/2019 14085 Bubble Merge

    86/151

    The Fourth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    4

    4

    N 8 did_swap true

  • 8/6/2019 14085 Bubble Merge

    87/151

    The Fourth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    4

    4

    N 8 did_swap true

    No Swap

  • 8/6/2019 14085 Bubble Merge

    88/151

    After Fourth Pass of Outer Loop

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    4

    5

    N 8 did_swap true

    Finished fourth Bubble Up

  • 8/6/2019 14085 Bubble Merge

    89/151

    The Fifth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    3

    1

    N 8 did_swap false

  • 8/6/2019 14085 Bubble Merge

    90/151

    The Fifth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    3

    1

    N 8 did_swap false

    No Swap

  • 8/6/2019 14085 Bubble Merge

    91/151

    The Fifth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    3

    2

    N 8 did_swap false

  • 8/6/2019 14085 Bubble Merge

    92/151

    The Fifth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    3

    2

    N 8 did_swap false

    No Swap

  • 8/6/2019 14085 Bubble Merge

    93/151

    The Fifth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    3

    3

    N 8 did_swap false

  • 8/6/2019 14085 Bubble Merge

    94/151

    The Fifth Bubble Up

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    3

    3

    N 8 did_swap false

    No Swap

  • 8/6/2019 14085 Bubble Merge

    95/151

    After Fifth Pass of Outer Loop

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    3

    4

    N 8 did_swap false

    Finished fifth Bubble Up

  • 8/6/2019 14085 Bubble Merge

    96/151

    Finished Early

    452314 33 42 676 98

    1 2 3 4 5 6 7 8

    to_do

    index

    3

    4

    N 8 did_swap false

    We didnt do any swapping,

    so all of the other elements

    must be correctly placed.

    We can skip the last two

    passes of the outer loop.

  • 8/6/2019 14085 Bubble Merge

    97/151

    Summary

    Bubble Up algorithm will move largestvalue to its correct location (to the right)

    Repeat Bubble Up until all elements are

    correctly placed: Maximum of N-1 times

    Can finish early ifno swapping occurs

    We reduce the number of elements we

    compare each time one is correctly placed

    LB

  • 8/6/2019 14085 Bubble Merge

    98/151

    Truth in CS Act

    NOBODY EVER USES BUBBLE SORT

    NOBODY

    NOT EVER

    BECAUSE IT IS EXTREMELY INEFFICIENT

  • 8/6/2019 14085 Bubble Merge

    99/151

    Questions?

  • 8/6/2019 14085 Bubble Merge

    100/151

    Mergesort

  • 8/6/2019 14085 Bubble Merge

    101/151

    Sorting

    Sorting takes an unordered collection and

    makes it an ordered one.

    512354277 101

    1 2 3 4 5 6

    5 12 35 42 77 101

    1 2 3 4 5 6

  • 8/6/2019 14085 Bubble Merge

    102/151

    M t

  • 8/6/2019 14085 Bubble Merge

    103/151

    Mergesort

    A divide-and-conquer algorithm:

    Divide the unsorted array into 2 halves until the

    sub-arrays only contain one element

    Merge the sub-problem solutions together:

    Compare the sub-arrays first elements Remove the smallest element and put it into

    the result array

    Continue the process until all elements have

    been put into the result array

    37 23 6 89 15 12 2 19

  • 8/6/2019 14085 Bubble Merge

    104/151

    Algorithm

    Mergesort(Passed an array)if array size > 1

    Divide array in half

    Call Mergesort on first half.

    Call Mergesort on second half.

    Merge two halves.

    Merge(Passed two arrays)

    Compare leading element in each array

    Select lower and place in new array.(If one input array is empty then place

    remainder of other array in output array)

    LB

  • 8/6/2019 14085 Bubble Merge

    105/151

    More TRUTH in CS

    We dont really pass in two arrays!

    We pass in one array with indicator variables which

    tell us where one set of data starts and finishes and

    where the other set of data starts and finishes.

    Honest.

    s1 f1 s2 f2

    LB

  • 8/6/2019 14085 Bubble Merge

    106/151

    Algorithm

    Mergesort(Passed an array)if array size > 1

    Divide array in half

    Call Mergesort on first half.

    Call Mergesort on second half.

    Merge two halves.

    Merge(Passed two arrays)

    Compare leading element in each array

    Select lower and place in new array.(If one input array is empty then place

    remainder of other array in output array)

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    107/151

    674523 14 6 3398 42

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    108/151

    674523 14 6 3398 42

    674523 14 6 3398 42

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    109/151

    674523 14 6 3398 42

    674523 14 6 3398 42

    4523 1498

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    110/151

    674523 14 6 3398 42

    674523 14 6 3398 42

    4523 1498

    2398

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    111/151

    674523 14 6 3398 42

    674523 14 6 3398 42

    4523 1498

    2398

    Merge

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    112/151

    674523 14 6 3398 42

    674523 14 6 3398 42

    4523 1498

    2398

    23

    Merge

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    113/151

    674523 14 6 3398 42

    4523 1498

    2398

    23 98

    Merge

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    114/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    23 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    115/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    Merge

    23 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    116/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    14

    Merge

    23 98

  • 8/6/2019 14085 Bubble Merge

    117/151

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    118/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    Merge

    98 451423

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    119/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    Merge

    98 14

    14

    23 45

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    120/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    Merge

    23 14

    14 23

    98 45

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    121/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    Merge

    23 98 4514

    14 23 45

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    122/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    Merge

    23 98 4514

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    123/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    23 98 4514

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    124/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676

    23 98 4514

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    125/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676

    Merge

    23 98 4514

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    126/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676

    6

    Merge

    23 98 4514

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    127/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676

    67

    Merge

    23 98 4514 6

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    128/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    23 98 4514 676

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    129/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    130/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    3323 98 4514 676

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    131/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    4223 98 4514 676 33

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    132/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676 4233

    14 23 45 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    133/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 6 4233

    14 23 45 98 6

    67

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    134/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 6 33

    14 23 45 98 6 33

    67 42

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    135/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 6 4233

    14 23 45 98 6 33 42

    67

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    136/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676 4233

    14 23 45 98 6 33 42 67

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    137/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676 4233

    23 45 98 33 42 6714 6

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    138/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676 4233

    23 45 98 6 42 67

    6

    14 33

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    139/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676 4233

    14 45 98 6 42 67

    6 14

    23 33

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    140/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676 4233

    14 23 98 6 42 67

    6 14 23

    45 33

  • 8/6/2019 14085 Bubble Merge

    141/151

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    142/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676 4233

    14 23 98 6 33 42

    6 14 23 33 42

    45 67

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    143/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676 4233

    14 23 45 6 33 42

    6 14 23 33 42 45

    98 67

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    144/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676 4233

    14 23 45 98 6 33 42 67

    6 14 23 33 42 45 67

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    145/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    Merge

    23 98 4514 676 4233

    14 23 45 98 6 33 42 67

    6 14 23 33 42 45 67 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    146/151

    674523 14 6 3398 42

    4523 1498

    2398 45 14

    676 33 42

    676 33 42

    23 98 4514 676 4233

    14 23 45 98 6 33 42 67

    6 14 23 33 42 45 67 98

    674523 14 6 3398 42

  • 8/6/2019 14085 Bubble Merge

    147/151

    6 14 23 33 42 45 67 98

    Summary

  • 8/6/2019 14085 Bubble Merge

    148/151

    Summary

    Divide the unsorted collection into two

    Until the sub-arrays only contain one

    element

    Then merge the sub-problem solutions

    together

  • 8/6/2019 14085 Bubble Merge

    149/151

    Questions?

  • 8/6/2019 14085 Bubble Merge

    150/151

    Review?

  • 8/6/2019 14085 Bubble Merge

    151/151