bubblesort.ppt

Upload: md-sagor

Post on 03-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 bubbleSort.ppt

    1/101

    How to make a cup of tea

  • 7/28/2019 bubbleSort.ppt

    2/101

    1. Put a cup of water to boil

    2. Take 3/4 piece of ginger

    3. Add the ginger to the water4. Then pour over some tea-leaves

    5. Wait until leaves are settled at the bottom of the pot.

    6. Pour into mug,add sugar and enjoy the tea !

  • 7/28/2019 bubbleSort.ppt

    3/101

    So.......Algorithm? In simple terms, is a series of instructions to solve a

    problem (complete a task)

    Problems can be in any form How to go from Curzon Hall to NewMarket

    Explain how to tie shoelaces to a five year old child

    Find largest number from a set of number

    Sort a given set of number

  • 7/28/2019 bubbleSort.ppt

    4/101

    Remember Computers only

    do things that you have told itto do !!!

  • 7/28/2019 bubbleSort.ppt

    5/101

    Example: Selection Problem

    Given a list of N numbers, determine the kth largest,

    where k N.

    Algorithm 1:

    (1) Read N numbers into an array

    (2) Sort the array in decreasing order by some

    simple algorithm

    (3) Return the element in position k

  • 7/28/2019 bubbleSort.ppt

    6/101

    Bubble Sort

  • 7/28/2019 bubbleSort.ppt

    7/101

    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

  • 7/28/2019 bubbleSort.ppt

    8/101

    "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

  • 7/28/2019 bubbleSort.ppt

    9/101

    "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

    Swap42 77

  • 7/28/2019 bubbleSort.ppt

    10/101

    "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

    Swap35 77

  • 7/28/2019 bubbleSort.ppt

    11/101

    "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

  • 7/28/2019 bubbleSort.ppt

    12/101

    "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

  • 7/28/2019 bubbleSort.ppt

    13/101

    "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

  • 7/28/2019 bubbleSort.ppt

    14/101

    "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

  • 7/28/2019 bubbleSort.ppt

    15/101

    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

  • 7/28/2019 bubbleSort.ppt

    16/101

    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.

  • 7/28/2019 bubbleSort.ppt

    17/101

    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 77

    1 2 3 4 5 6

    101

    4235125 77

    1 2 3 4 5 6

    101

    N-

    1

  • 7/28/2019 bubbleSort.ppt

    18/101

    The Bubble Up Algorithm

    n = totalData

    for i=1 to i=n-1

    for j=1 to j=n-1

    if number[j]>number[j+1]

    swap(number[j],number[j+1])

  • 7/28/2019 bubbleSort.ppt

    19/101

    No, Swap isnt built in.

    Procedure Swap(a, b)

    t = a

    a = bb = t

    LB

  • 7/28/2019 bubbleSort.ppt

    20/101

    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

  • 7/28/2019 bubbleSort.ppt

    21/101

    Reducing the Number of Comparisons

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

    For example:

    This is the 4thbubble up

    MAX is 6

    Thus we have 2 comparisons to do

    4253512 771 2 3 4 5 6

    101

  • 7/28/2019 bubbleSort.ppt

    22/101

    Putting It All Together

  • 7/28/2019 bubbleSort.ppt

    23/101

    The Bubble Up Algorithm (Extended

    )n = totalData

    for i=1 to i=n-1

    for j=1 to j=n-1-iif number[j]>number[j+1]

    swap(number[j],number[j+1])

  • 7/28/2019 bubbleSort.ppt

    24/101

    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

  • 7/28/2019 bubbleSort.ppt

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

  • 7/28/2019 bubbleSort.ppt

    26/101

    The Bubble Up Algorithm (Extended

    )n = totalData

    finish = false

    for i=1 to i=n-1

    if finish

    break

    finish = true

    for j=1 to j=n-1-iif number[j]>number[j+1]

    swap(number[j],number[j+1])

    finish = false

  • 7/28/2019 bubbleSort.ppt

    27/101

    An Animated Example

    674523 14 6 3398 42

    1 2 3 4 5 6 7 8

    to_do

    index

    7

    N 8 did_swap true

  • 7/28/2019 bubbleSort.ppt

    28/101

    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

  • 7/28/2019 bubbleSort.ppt

    29/101

    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

  • 7/28/2019 bubbleSort.ppt

    30/101

    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

  • 7/28/2019 bubbleSort.ppt

    31/101

    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

  • 7/28/2019 bubbleSort.ppt

    32/101

    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

  • 7/28/2019 bubbleSort.ppt

    33/101

    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

  • 7/28/2019 bubbleSort.ppt

    34/101

    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

  • 7/28/2019 bubbleSort.ppt

    35/101

    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

  • 7/28/2019 bubbleSort.ppt

    36/101

    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

  • 7/28/2019 bubbleSort.ppt

    37/101

    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

  • 7/28/2019 bubbleSort.ppt

    38/101

    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

  • 7/28/2019 bubbleSort.ppt

    39/101

    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

  • 7/28/2019 bubbleSort.ppt

    40/101

    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

  • 7/28/2019 bubbleSort.ppt

    41/101

    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

  • 7/28/2019 bubbleSort.ppt

    42/101

    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

  • 7/28/2019 bubbleSort.ppt

    43/101

    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

  • 7/28/2019 bubbleSort.ppt

    44/101

    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

  • 7/28/2019 bubbleSort.ppt

    45/101

    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

  • 7/28/2019 bubbleSort.ppt

    46/101

    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

  • 7/28/2019 bubbleSort.ppt

    47/101

    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

  • 7/28/2019 bubbleSort.ppt

    48/101

    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

  • 7/28/2019 bubbleSort.ppt

    49/101

    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

  • 7/28/2019 bubbleSort.ppt

    50/101

    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

  • 7/28/2019 bubbleSort.ppt

    51/101

    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

  • 7/28/2019 bubbleSort.ppt

    52/101

    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

  • 7/28/2019 bubbleSort.ppt

    53/101

    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

  • 7/28/2019 bubbleSort.ppt

    54/101

    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

  • 7/28/2019 bubbleSort.ppt

    55/101

    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

  • 7/28/2019 bubbleSort.ppt

    56/101

    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

  • 7/28/2019 bubbleSort.ppt

    57/101

    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

  • 7/28/2019 bubbleSort.ppt

    58/101

    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

  • 7/28/2019 bubbleSort.ppt

    59/101

    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

  • 7/28/2019 bubbleSort.ppt

    60/101

    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

  • 7/28/2019 bubbleSort.ppt

    61/101

    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

  • 7/28/2019 bubbleSort.ppt

    62/101

    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

  • 7/28/2019 bubbleSort.ppt

    63/101

    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

  • 7/28/2019 bubbleSort.ppt

    64/101

    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

  • 7/28/2019 bubbleSort.ppt

    65/101

    The Second Bubble Up

    42614 45 33 6723 98

    1 2 3 4 5 6 7 8

    to_do

    index

    6

    6

    N 8 did_swap true

    Swap

  • 7/28/2019 bubbleSort.ppt

    66/101

    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

  • 7/28/2019 bubbleSort.ppt

    67/101

    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

  • 7/28/2019 bubbleSort.ppt

    68/101

    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

  • 7/28/2019 bubbleSort.ppt

    69/101

    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

  • 7/28/2019 bubbleSort.ppt

    70/101

    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

  • 7/28/2019 bubbleSort.ppt

    71/101

    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

  • 7/28/2019 bubbleSort.ppt

    72/101

    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

  • 7/28/2019 bubbleSort.ppt

    73/101

    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

  • 7/28/2019 bubbleSort.ppt

    74/101

    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

  • 7/28/2019 bubbleSort.ppt

    75/101

    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

  • 7/28/2019 bubbleSort.ppt

    76/101

    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

  • 7/28/2019 bubbleSort.ppt

    77/101

    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

  • 7/28/2019 bubbleSort.ppt

    78/101

    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

  • 7/28/2019 bubbleSort.ppt

    79/101

    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

  • 7/28/2019 bubbleSort.ppt

    80/101

    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

  • 7/28/2019 bubbleSort.ppt

    81/101

    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

  • 7/28/2019 bubbleSort.ppt

    82/101

    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

  • 7/28/2019 bubbleSort.ppt

    83/101

    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

  • 7/28/2019 bubbleSort.ppt

    84/101

    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

  • 7/28/2019 bubbleSort.ppt

    85/101

    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

  • 7/28/2019 bubbleSort.ppt

    86/101

    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

  • 7/28/2019 bubbleSort.ppt

    87/101

    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

  • 7/28/2019 bubbleSort.ppt

    88/101

    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

  • 7/28/2019 bubbleSort.ppt

    89/101

    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

  • 7/28/2019 bubbleSort.ppt

    90/101

    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

  • 7/28/2019 bubbleSort.ppt

    91/101

    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

  • 7/28/2019 bubbleSort.ppt

    92/101

    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

  • 7/28/2019 bubbleSort.ppt

    93/101

    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

  • 7/28/2019 bubbleSort.ppt

    94/101

    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

  • 7/28/2019 bubbleSort.ppt

    95/101

    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

  • 7/28/2019 bubbleSort.ppt

    96/101

    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

  • 7/28/2019 bubbleSort.ppt

    97/101

    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

  • 7/28/2019 bubbleSort.ppt

    98/101

    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

  • 7/28/2019 bubbleSort.ppt

    99/101

    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.

  • 7/28/2019 bubbleSort.ppt

    100/101

    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

  • 7/28/2019 bubbleSort.ppt

    101/101

    Truth in CS Act

    NOBODY EVER USES BUBBLE SORT

    NOBODY

    NOT EVER

    BECAUSE IT IS EXTREMELY INEFFICIENT