bubble sorting

Upload: kanika-garg

Post on 14-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Bubble sorting

    1/22

    SUBMITTED TO-

    Santosh Kumar

    SUBITTED BY-Kanika Garg4th sem6212/11

  • 7/30/2019 Bubble sorting

    2/22

    Sorting takes an unordered collectionand makes it an ordered one.

    512354277 101

    1 2 3 4 5 65 12 35 42 77 101

    1 2 3 4 5 6

  • 7/30/2019 Bubble sorting

    3/22

    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 1011 2 3 4 5 6

  • 7/30/2019 Bubble sorting

    4/22

    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 1011 2 3 4 5 6

    Swap

    42 77

  • 7/30/2019 Bubble sorting

    5/22

    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 1011 2 3 4 5 6

    Swap

    35 77

  • 7/30/2019 Bubble sorting

    6/22

    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 1011 2 3 4 5 6

    Swap

    12 77

  • 7/30/2019 Bubble sorting

    7/22

    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 1011 2 3 4 5 6

    No need to swap

  • 7/30/2019 Bubble sorting

    8/22

    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 1011 2 3 4 5 6

    Swap5 101

  • 7/30/2019 Bubble sorting

    9/22

    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 51 2 3 4 5 6

    101

    Largest value correctly placed

  • 7/30/2019 Bubble sorting

    10/22

    index A[index + 1]) then

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

    endifindex

  • 7/30/2019 Bubble sorting

    11/22

    Notice that only the largest value iscorrectly placed All other values are still out of order So we need to repeat this process

    77123542 51 2 3 4 5 6

    101

    Largest value correctly placed

  • 7/30/2019 Bubble sorting

    12/22

    If we have N elements And if each time we bubble anelement, we place it in its correct

    location Then we repeat the bubble upprocess N 1 times. This guarantees well correctlyplace all N elements.

  • 7/30/2019 Bubble sorting

    13/22

    77123542 51 2 3 4 5 6

    101

    5421235 771 2 3 4 5 6

    101

    4253512 771 2 3 4 5 6

    101

    4235512 771 2 3 4 5 6

    101

    4235125 771 2 3 4 5 6

    101

    N

    -1

  • 7/30/2019 Bubble sorting

    14/22

    12354277 1011 2 3 4 5 6

    5

    77123542 51 2 3 4 5 6

    101

    5421235 771 2 3 4 5 6

    101

    4253512 771 2 3 4 5 6

    101

    4235512 771 2 3 4 5 6

    101

  • 7/30/2019 Bubble sorting

    15/22

    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 6101

  • 7/30/2019 Bubble sorting

    16/22

  • 7/30/2019 Bubble sorting

    17/22

    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

  • 7/30/2019 Bubble sorting

    18/22

    What if the collection was already sorted? What if only a few elements were out ofplace and after a couple of bubble ups,

    the collection was sorted? We want to be able to detect thisand stop early!

    4235125 771 2 3 4 5 6101

  • 7/30/2019 Bubble sorting

    19/22

    We can use a boolean variable to determine ifany swapping occurred during the bubbleup. If no swapping occurred, then we know thatthe collection is already sorted! This boolean flag needs to be reset aftereach bubble up.

  • 7/30/2019 Bubble sorting

    20/22

    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 if no swapping occurs

    We reduce the number of elements wecompare each time one is correctlyplaced

  • 7/30/2019 Bubble sorting

    21/22

    NOBODY EVER USES BUBBLE SORT NOBODY NOT EVER BECAUSE IT IS EXTREMELY INEFFICIENT

    LB

  • 7/30/2019 Bubble sorting

    22/22