04. Selection Sort

download 04. Selection Sort

of 20

Transcript of 04. Selection Sort

  • 8/8/2019 04. Selection Sort

    1/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 11

    DATASTRUCTURES

    MAHESH GOYANI

    MAHATMAGANDHI INSTITUE OF TECHNICALEDUCATION & RESEARCH CENTER

    [email protected]

  • 8/8/2019 04. Selection Sort

    2/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 22

    SELECTIONSORT

  • 8/8/2019 04. Selection Sort

    3/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 33

    In plain English: Find the smallest element and place itin thefirst position, swapping places with the original first element;continue with successive elements

    In pseudo code:

    for each element from first to last

    find smallest successive element

    swap the elements

    TERMINOLOGY

  • 8/8/2019 04. Selection Sort

    4/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 44

    5 8 20 10 7

    5 7 20 10 8

    5 7 8 10 20

    20 8 5 10 7

    5 7 8 10 20

    Swapping Action

  • 8/8/2019 04. Selection Sort

    5/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 55

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    Start by finding the smallest entry.

    Swap the smallest entry with the first entry.

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    [0] [1] [2] [3] [4] [5]

  • 8/8/2019 04. Selection Sort

    6/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 66

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    [0] [1] [2] [3] [4] [5]

  • 8/8/2019 04. Selection Sort

    7/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 77

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    Part of the array is now sorted.

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    Sorted side Unsorted side

    [0] [1] [2] [3] [4] [5]

  • 8/8/2019 04. Selection Sort

    8/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 88

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    Find the smallest elementin the unsorted side.

    Sorted side Unsorted side

    [0] [1] [2] [3] [4] [5]

  • 8/8/2019 04. Selection Sort

    9/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 99

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    Find the smallest elementin the unsorted side.

    Swap with the front of the unsorted side.

    Sorted side Unsorted side

    [0] [1] [2] [3] [4] [5]

  • 8/8/2019 04. Selection Sort

    10/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1010

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    We have increased the size of the sorted side by oneelement.

    Sorted side Unsorted side

    [0] [1] [2] [3] [4] [5]

  • 8/8/2019 04. Selection Sort

    11/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1111

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    The processThe processcontinues...!!continues...!!

    Sorted side Unsorted side

    Smallest

    from

    unsorted

    [0] [1] [2] [3] [4] [5]

  • 8/8/2019 04. Selection Sort

    12/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1212

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    Sorted side Unsorted side

    [0] [1] [2] [3] [4] [5]

    The processThe processcontinues...!!continues...!!

  • 8/8/2019 04. Selection Sort

    13/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1313

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    Sorted side Unsorted sideSorted side

    is bigger

    [0] [1] [2] [3] [4] [5]

    The processThe processcontinues...!!continues...!!

  • 8/8/2019 04. Selection Sort

    14/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1414

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6]

    Sorted side Unsorted side

    [0] [1] [2] [3] [4] [5]

  • 8/8/2019 04. Selection Sort

    15/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1515

    0

    10

    20

    30

    40

    50

    60

    70

    [1] [2] [3] [4] [5] [6][0] [1] [2] [3] [4] [5]

    SORTED..!!

  • 8/8/2019 04. Selection Sort

    16/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1616

    22 45 13 97 432213

    45 97 432213 4522

    22 45 974313

    97 4313 4522 4543

    22 97 454313 45 97

    DONE..!!

    Simulation

  • 8/8/2019 04. Selection Sort

    17/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1717

    1212 88881212121288

    661212666612121212 17172222 881414

    66 17172222 1414

    Simulation

  • 8/8/2019 04. Selection Sort

    18/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1818

    14141414 17172222 171722222222171788

    22221212 1212222288 1212222266 17171414

    66 1212

    Simulation

  • 8/8/2019 04. Selection Sort

    19/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1919

    7 2 8 5 4

    2 7 8 5 4

    2 4 8 5 7

    2 4 5 8 7

    2 4 5 7 8

    Simulation

  • 8/8/2019 04. Selection Sort

    20/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2020

    Analysis of the code shows that the outer loop executes n-1 times;

    for the firstiteration of the outer loop, the inner loop executes n-1times

    for the seconditeration of the outer loop, the inner loop executesn-2 times

    etc

    for the lastiteration of the outer loop, the inner loop executes 1time

    thus the execution time is (n-1) + (n-2) + (n-3) + + 3 + 2 +

    1

    the sum of this series is (n2 - n) / 2

    selection sortis O(n2 )

    COMPLEXITY