Logika Dan Algoritma, Pertemuan 8

Embed Size (px)

Citation preview

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    1/18

    LOGICS AND ALGORITHMSby : Hendr a Sup rayog i, S.Kom ., ST.

    The 8th Subject

    Sorting

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    2/18

    WHAT IS IT?

    Process to make a random

    array sorted.

    45 77 2 10 -7 29 12 72 80 53 41 22

    -7 2 10 12 22 29 41 45 53 72 77 80

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    3/18

    WHY?

    ...to make the searching

    process faster, since binary

    search can be performed on asorted array only.

    45 77 2 10 -7 29 12 72 80 53 41 22

    failed

    -8 14 24 29 31 45 52 54 60 72 76 81

    success

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    4/18

    THE METHODS

    how to perform?

    the simplest one, the bubble sort.

    merge sort. maximum successive sort.

    quick sort.

    etc.

    BUBBLE SORT

    MAXIMUM

    SUCCESSIVE SORT

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    5/18

    BUBBLE SORT

    45 77 2 10 -7 29

    start from 0 to N-2, perform the following steps.

    compare the current element to the next element.

    if the current element is greater than the next, then swap them. repeat the entire process (N-1) times.

    45 77 2 10 -7 29

    45 2 77 10 -7 29

    45 2 10 77 -7 29

    45 2 10 -7 77 29

    45 2 10 -7 29 77

    1

    pass

    1st progress

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    6/18

    BUBBLE SORT (CONT'D)

    45 2 10 -7 29 77

    2 45 10 -7 29 77

    2 10 45 -7 29 77

    2 10 -7 45 29 77

    2 10 -7 29 45 77

    2 10 -7 29 45 77

    1

    pass 2nd progress

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    7/18

    BUBBLE SORT (CONT'D)

    2 10 -7 29 45 77

    2 10 -7 29 45 77

    2 -7 10 29 45 77

    2 -7 10 29 45 77

    2 -7 10 29 45 77

    2 -7 10 29 45 77

    1

    pass 3rd progress

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    8/18

    BUBBLE SORT (CONT'D)

    2 -7 10 29 45 77

    -7 2 10 29 45 77

    -7 2 10 29 45 77

    -7 2 10 29 45 77

    -7 2 10 29 45 77

    -7 2 10 29 45 77

    1

    pass 4th progress

    OK, in this case we need only 4 passes to make the sorted one.

    The next step, in this case, is uneccessary.

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    9/18

    BUBBLE SORT (CONT'D)

    2 -7 10 29 45 77

    -7 2 10 29 45 77

    -7 2 10 29 45 77

    -7 2 10 29 45 77

    -7 2 10 29 45 77

    -7 2 10 29 45 77

    1

    pass 5th progress

    In a worst case, this pass must be performed.

    Hence, the case in which initial position of the lowest value in the most right element.

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    10/18

    BUBBLE SORT (PSEUDOCODE)

    PROCEDURE SWAP(A, B)

    TEMP AA

    BB TEMP

    END PROC

    PROC BUBBLESORT(DATA)

    FOR I 0 TO LENGTH(DATA) 2 DOFOR J 0 TO LENGTH(DATA) 2 DOIF DATA[J] > DATA[J + 1] THEN

    SWAP(DATA[J], DATA[J + 1])

    END IF

    END FOR

    END FOR

    END PROC

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    11/18

    MAXIMUM SUCCESSIVE SORT

    45 77 2 10 -7 29

    give initial condition for X = N 1.

    compare all elements from 0 to X.

    find the greatest, swap it into element X. subtract X with 1.

    repeat from 2nd step until X reaches 0.

    45 29 2 10 -7 771pass

    1st progress

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    12/18

    MAXIMUM SUCCESSIVE SORT (CONT'D)

    45 29 2 10 -7 77

    -7 29 2 10 45 771

    pass

    2nd progress

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    13/18

    MAXIMUM SUCCESSIVE SORT (CONT'D)

    -7 29 2 10 45 77

    -7 10 2 29 45 771

    pass

    3rd progress

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    14/18

    MAXIMUM SUCCESSIVE SORT (CONT'D)

    -7 10 2 29 45 77

    -7 2 10 29 45 771

    pass

    4th progress

    OK, in this case we need only 4 passes to make the sorted one.

    The next step, in this case, is uneccessary.

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    15/18

    MAXIMUM SUCCESSIVE SORT (CONT'D)

    -7 2 10 29 45 77

    -7 2 10 29 45 771

    pass

    5th progress

    In a worst case, this pass must be performed.

    Hence, the case in which initial position of the lowest value in the most right element.

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    16/18

    MAXIMUM SUCCESSIVE SORT (PSEUDOCODE)

    PROCEDURE SWAP(A, B)

    TEMP AA

    BB TEMP

    END PROC

    PROC MAXIMUMSUCCESSIVESORT(DATA)

    FOR X HIGH(DATA) DOWNTO 1 DOMAXPOS 0FOR I 1 TO X DOIF DATA[I] > DATA[MAXPOS] THEN

    MAXPOS IEND IF

    END FOR

    SWAP(DATA[MAXPOS], DATA[X])

    END FOR

    END PROC

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    17/18

    ROUTINE TASKS

    you have array which contains 300 elements, and every element is a record

    structured with name, address, reg num, score, and age.

    fill them with random value, make sure no duplicate in reg num, and makesure the reg num is absolutely randomized (unsorted).

    create a function (Sort_By_RegNum) to sort this array by reg num.

  • 7/30/2019 Logika Dan Algoritma, Pertemuan 8

    18/18

    WHATS NEXT?

    final task, just prepare for it...