selectionsort with java

Upload: arpit-gupta

Post on 03-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 SelectionSort With Java

    1/34

    The Selection Sort

    Mr. Dave ClausenMr. Dave Clausen

    La Caada High SchoolLa Caada High School

  • 7/28/2019 SelectionSort With Java

    2/34

    Mr. Dave Clausen 2

    The Selection SortDescription

    The Selection Sort searches (linear search) all of The Selection Sort searches (linear search) all of the elements in a list until it finds the smallestthe elements in a list until it finds the smallest

    element. It swaps this with the first element inelement. It swaps this with the first element inthe list. Next it finds the smallest of the remainingthe list. Next it finds the smallest of the remainingelements, and swaps it with the second element.elements, and swaps it with the second element.

    Repeat this process until you compare only the lastRepeat this process until you compare only the lasttwo elements in the list.two elements in the list.

  • 7/28/2019 SelectionSort With Java

    3/34

    Mr. Dave Clausen 3

    The Selection SortAlgorithm

    For each index positionFor each index position i

    Find the smallest data value in the arrayFind the smallest data value in the arrayfrom positionsfrom positions i through lengththrough length -- 1, where1, wherelength is the number of data valueslength is the number of data values

    stored.stored. Exchange (swap) the smallest value withExchange (swap) the smallest value with

    the value at positionthe value at positioni..

  • 7/28/2019 SelectionSort With Java

    4/34

    Mr. Dave Clausen 4

    6

    21

    35

    4

    We start by searching for thesmallest element in the List.

    A Selection Sort Example

    Smallest ?

  • 7/28/2019 SelectionSort With Java

    5/34

    Mr. Dave Clausen 5

    6

    21

    35

    4

    A Selection Sort Example

    Smallest ?

  • 7/28/2019 SelectionSort With Java

    6/34

    Mr. Dave Clausen 6

    6

    21

    35

    4

    A Selection Sort Example

    Smallest !

  • 7/28/2019 SelectionSort With Java

    7/34

    Mr. Dave Clausen 7

    6

    21

    35

    4

    A Selection Sort Example

    Swap

  • 7/28/2019 SelectionSort With Java

    8/34

    Mr. Dave Clausen 8

    1

    26

    35

    4

    A Selection Sort Example

    Swapped

  • 7/28/2019 SelectionSort With Java

    9/34

    Mr. Dave Clausen 9

    1

    26

    35

    4

    A Selection Sort Example

    After the smallest elementis in the first position, wecontinue searching withthe second element and look for the next smallestelement.

    Smallest ?

  • 7/28/2019 SelectionSort With Java

    10/34

    Mr. Dave Clausen 10

    1

    26

    35

    4

    A Selection Sort Example

    In this special case, thenext smallest element is inthe second positionalready. Swapping keepsit in this position.

    Smallest !

  • 7/28/2019 SelectionSort With Java

    11/34

    Mr. Dave Clausen 11

    1

    26

    35

    4

    A Selection Sort Example

    Swapping keeps it in this position.

    Swapped

  • 7/28/2019 SelectionSort With Java

    12/34

    Mr. Dave Clausen 12

    1

    26

    35

    4

    A Selection Sort Example

    After the next smallest element isin the second position, we continue

    searching with the third elementand look for the next smallestelement.

    Smallest ?

  • 7/28/2019 SelectionSort With Java

    13/34

    Mr. Dave Clausen 13

    1

    26

    35

    4

    A Selection Sort Example

    Smallest !

  • 7/28/2019 SelectionSort With Java

    14/34

    Mr. Dave Clausen 14

    1

    26

    35

    4

    A Selection Sort Example

    Swap

  • 7/28/2019 SelectionSort With Java

    15/34

    Mr. Dave Clausen 15

    1

    23

    65

    4

    A Selection Sort Example

    Swapped

  • 7/28/2019 SelectionSort With Java

    16/34

    Mr. Dave Clausen 16

    1

    23

    65

    4

    A Selection Sort Example

    Smallest ?

  • 7/28/2019 SelectionSort With Java

    17/34

    Mr. Dave Clausen 17

    1

    23

    65

    4

    A Selection Sort Example

    Smallest ?

  • 7/28/2019 SelectionSort With Java

    18/34

    Mr. Dave Clausen 18

    1

    23

    65

    4

    A Selection Sort Example

    Smallest !

  • 7/28/2019 SelectionSort With Java

    19/34

    Mr. Dave Clausen 19

    1

    23

    65

    4

    A Selection Sort Example

    Swap

  • 7/28/2019 SelectionSort With Java

    20/34

    Mr. Dave Clausen 20

    1

    23

    45

    6

    A Selection Sort Example

    Swapped

  • 7/28/2019 SelectionSort With Java

    21/34

    Mr. Dave Clausen 21

    1

    23

    45

    6

    A Selection Sort Example

    The last two elements are inorder, so no swap is necessary.

  • 7/28/2019 SelectionSort With Java

    22/34

    Mr. Dave Clausen 22

    What Swapping Means6

    21

    35

    4

    TEMP

    Place the first element intothe Temporary Variable.

    6

  • 7/28/2019 SelectionSort With Java

    23/34

    Mr. Dave Clausen 23

    What Swapping Means1

    21

    35

    4

    TEMP

    Replace the first elementwith the value of the

    smallest element.

    6

  • 7/28/2019 SelectionSort With Java

    24/34

    Mr. Dave Clausen 24

    What Swapping Means1

    26

    35

    4

    TEMP

    Replace the third element(in this example) with the

    Temporary Variable.

    6

  • 7/28/2019 SelectionSort With Java

    25/34

    Mr. Dave Clausen 25

    Java Source Code: Selection Sort public static void selectionSort(int[] list){

    for (int index = 0; index < list.length - 1; index ++){

    int minIndex = findMinimum(list, index );if (minIndex != index )swap(list, index , minIndex);

    }}

  • 7/28/2019 SelectionSort With Java

    26/34

    Mr. Dave Clausen 26

    Java Code For Find Minimum

    p public static int findMinimum(int[] list, int first){

    int minIndex = first;

    for (int index = first + 1; index < list.length; index ++)if (list[index] < list[minIndex])

    minIndex = index ;

    return minIndex;}

  • 7/28/2019 SelectionSort With Java

    27/34

    Mr. Dave Clausen 27

    Java Code for Swap Procedure public static void swap(int[] list, int x, int y){

    int temp = list[x];

    list[x] = list[y];list[y] = temp;

    }

  • 7/28/2019 SelectionSort With Java

    28/34

    Mr. Dave Clausen 28

    C ++ Code For Selection Sortvoid Selection_Sort (void Selection_Sort ( apvector apvector & v) & v){{

    int min_index = 0;int min_index = 0;

    for (int index = 0; index < vfor (int index = 0; index < v .. length( )length( ) -- 1; ++index)1; ++index){{

    min_index = Find_Minimum (v, index);min_index = Find_Minimum (v, index);

    if (min_index ! = index)if (min_index ! = index)

    Swap_Data ( v [index], v [min_index])Swap_Data ( v [index], v [min_index])

    }}

    }}

    // Selection_Sort// Selection_Sort

  • 7/28/2019 SelectionSort With Java

    29/34

    Mr. Dave Clausen 29

    C ++ Code For Find Minimum

    int Find_Minimum (constint Find_Minimum (const apvector apvector & v, int first) & v, int first)

    {{

    int min_index = first;int min_index = first;for (int index = first + 1; index < vfor (int index = first + 1; index < v .. length( ); ++index)length( ); ++index)

    if ( v[index] < v[min_index])if ( v[index] < v[min_index])

    min_index = index;min_index = index;

    return min_index;return min_index;

    }}// Find_Minimum// Find_Minimum

  • 7/28/2019 SelectionSort With Java

    30/34

    Mr. Dave Clausen 30

    C ++ Code for Swap Procedure

    void Swap_Data (int &number1, int &number2)void Swap_Data (int &number1, int &number2)

    {{

    int temp;int temp;

    temp = number1;temp = number1;number1 = number2;number1 = number2;

    number2 = temp;number2 = temp;

    }}// End of Swap_Data function// End of Swap_Data function

  • 7/28/2019 SelectionSort With Java

    31/34

    Mr. Dave Clausen 31

    Pascal Code For Selection Sort procedure procedure SelectionSortSelectionSort ((var IntArrayvar IntArray :: IntNumbersIntNumbers ););

    var var element,element, SmallIndexSmallIndex , index: integer;, index: integer;

    begin begin

    for element := 1 to (for element := 1 to ( MaxNumMaxNum -- 1) do1) do begin begin

    SmallIndexSmallIndex := element;:= element;

    for index := (element + 1) tofor index := (element + 1) to MaxNumMaxNum dodo

    if if IntArrayIntArray [index]

  • 7/28/2019 SelectionSort With Java

    32/34

    Mr. Dave Clausen 32

    Pascal Code for Swap Procedure procedure Swap ( procedure Swap ( var var number1, number2: integer);number1, number2: integer);

    var var

    temp: integer;temp: integer;

    begin begintemp := number1;temp := number1;

    number1 := number2;number1 := number2;

    number2 := tempnumber2 := tempend; {Swap}end; {Swap}

  • 7/28/2019 SelectionSort With Java

    33/34

    Mr. Dave Clausen 33

    BASIC Code For Selection Sort8000 REM #################8000 REM #################8010 REM Selection Sort8010 REM Selection Sort

    8020 REM #################8020 REM #################

    8030 FOR ELEMENT = 1 TO MAXNUM8030 FOR ELEMENT = 1 TO MAXNUM -- 118040 ::8040 :: SmallIndexSmallIndex = element= element

    8050 ::FOR INDEX = (element + 1) TO MAXNUM8050 ::FOR INDEX = (element + 1) TO MAXNUM

    8060 ::::::IF N (INDEX) < N (8060 ::::::IF N (INDEX) < N ( SmallIndexSmallIndex ) THEN) THEN SmallIndexSmallIndex = INDEX= INDEX

    8070 ::NEXT INDEX8070 ::NEXT INDEX

    8080 ::TEMP = N (element)8080 ::TEMP = N (element)

    8090 ::N (element) = N (8090 ::N (element) = N ( SmallIndexSmallIndex ))

    8100 ::N (8100 ::N ( SmallIndexSmallIndex ) = TEMP) = TEMP8110 NEXT ELEMENT8110 NEXT ELEMENT

    8120 RETURN8120 RETURN

  • 7/28/2019 SelectionSort With Java

    34/34

    Mr. Dave Clausen 34

    Big - O Notation

    BigBig

    --

    O notation is used to describe the efficiencyO notation is used to describe the efficiency

    of a search or sort. The actual time necessary toof a search or sort. The actual time necessary tocomplete the sort varies according to the speed of complete the sort varies according to the speed of

    your system.your system. BigBig -- O notation is an approximateO notation is an approximatemathematical formula to determine how manymathematical formula to determine how manyoperations are necessary to perform the search or operations are necessary to perform the search or

    sort. The Bigsort. The Big -- O notation for the Selection Sort isO notation for the Selection Sort isO(nO(n 22), because it takes approximately n), because it takes approximately n 22 passes to passes tosort the elements.sort the elements.