Anda di halaman 1dari 18

The 8th Subject

Sorting

LOGICS AND ALGORITHMS


by: Hendra Suprayogi, S.Kom., ST.

WHAT IS IT? Process to make a random array sorted.

45

77

10

-7

29

12

72

80

53

41

22

-7

10

12

22

29

41

45

53

72

77

80

WHY? ...to make the searching process faster, since binary search can be performed on a sorted array only.

45

77

10

-7

29

12

72

80

53

41

22

failed

-8

14

24

29

31

45

52

54

60

72

76

81

success

THE METHODS how to perform?


the simplest one, the bubble sort. merge sort. maximum successive sort. quick sort. etc.

BUBBLE SORT

MAXIMUM SUCCESSIVE SORT

BUBBLE SORT

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

10

-7

29

45

77

10

-7

29

1 pass

45

77

10

-7

29

1st progress

45

10

77

-7

29

45

10

-7

77

29

45

10

-7

29

77

BUBBLE SORT (CONT'D)

45

10

-7

29

77

45

10

-7

29

77

1 pass

10

45

-7

29

77

2nd progress

10

-7

45

29

77

10

-7

29

45

77

10

-7

29

45

77

BUBBLE SORT (CONT'D)

10

-7

29

45

77

10

-7

29

45

77

1 pass

-7

10

29

45

77

3rd progress

-7

10

29

45

77

-7

10

29

45

77

-7

10

29

45

77

BUBBLE SORT (CONT'D)

-7

10

29

45

77

-7

10

29

45

77

1 pass

-7

10

29

45

77

4th progress

-7

10

29

45

77

-7

10

29

45

77

-7

10

29

45

77

OK, in this case we need only 4 passes to make the sorted one. The next step, in this case, is uneccessary.

BUBBLE SORT (CONT'D)

-7

10

29

45

77

-7

10

29

45

77

1 pass

-7

10

29

45

77

5th progress

-7

10

29

45

77

-7

10

29

45

77

-7

10

29

45

77

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.

BUBBLE SORT (PSEUDOCODE)


PROCEDURE SWAP(A, B) TEMP A A B B TEMP END PROC PROC BUBBLESORT(DATA) FOR I 0 TO LENGTH(DATA) FOR J 0 TO LENGTH(DATA) IF DATA[J] > DATA[J + 1] SWAP(DATA[J], DATA[J + END IF END FOR END FOR END PROC

2 DO 2 DO THEN 1])

MAXIMUM SUCCESSIVE SORT

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.

1 pass

45

77

10

-7

29

1st progress
45 29 2 10 -7 77

MAXIMUM SUCCESSIVE SORT (CONT'D)

1 pass

45

29

10

-7

77

2nd progress
-7 29 2 10 45 77

MAXIMUM SUCCESSIVE SORT (CONT'D)

1 pass

-7

29

10

45

77

3rd progress
-7 10 2 29 45 77

MAXIMUM SUCCESSIVE SORT (CONT'D)

1 pass

-7

10

29

45

77

4th progress
-7 2 10 29 45 77

OK, in this case we need only 4 passes to make the sorted one. The next step, in this case, is uneccessary.

MAXIMUM SUCCESSIVE SORT (CONT'D)

1 pass

-7

10

29

45

77

5th progress
-7 2 10 29 45 77

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.

MAXIMUM SUCCESSIVE SORT (PSEUDOCODE)


PROCEDURE SWAP(A, B) TEMP A A B B TEMP END PROC PROC MAXIMUMSUCCESSIVESORT(DATA) FOR X HIGH(DATA) DOWNTO 1 DO MAXPOS 0 FOR I 1 TO X DO IF DATA[I] > DATA[MAXPOS] THEN MAXPOS I END IF END FOR SWAP(DATA[MAXPOS], DATA[X]) END FOR END PROC

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 make sure the reg num is absolutely randomized (unsorted). create a function (Sort_By_RegNum) to sort this array by reg num.

WHATS NEXT?

final task, just prepare for it...

Anda mungkin juga menyukai