1 / 39

Chapter 2.9

Chapter 2.9. Sorting Arrays. A set of records is given Each record is identified by a certain key One wants to sort the records according to an order relation defined for the key. Sort Algorithms. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort

vidor
Télécharger la présentation

Chapter 2.9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2.9 Sorting Arrays

  2. A set of records is given Each record is identified by a certain key One wants to sort the records according to an order relation defined for the key. Sort Algorithms

  3. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms

  4. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms

  5. Alphabetical sorting of names is quite common Names can contain upper- and lower case letters Names can also contain spaces and special signs The sorting algorithm should not take into account The case of the letters The special signs and spaces The names will be transformed into keys by a special procedure taking into account the above specifications. Key Preparation

  6. Alphabetical Key PROCEDURE MakeKey(VAR Names, Key: ARRAY OF CHAR); VAR n,k: CARDINAL; Offset: INTEGER; BEGIN Offset := ORD(“a”)-ORD(“A”); Lname := HIGH(Name); Lkey := HIGH(Key); n := 0; k := 0; REPEAT c := Name[n]; IF (c>=“A”)AND(c<=“Z”) (* Uppercase Letter *) THEN Key[k] := c; n:=n+1; k:=k+1; ELSIF (c>=“a”) AND (c<=“z”) (* Lowercase Letter *) THEN Key[k] := CHR(ORD(c)-Offset); n:=n+1; k:=k+1; ELSE n:=n+1 (* Not a Letter, ignore *) END; (* IF *) UNTIL (n> Lname) OR (k> Lkey); FOR k := k TO Lkey DO Key[k] := “ “ END; END MakeKey;

  7. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms

  8. Array Sort • PROCEDURE Sort • One VAR parameter: • The Array to be sorted • Contains n Items • Index in the range 0..n • Item with index 0 is not used • After execution of Sort, • The Array is sorted according to the order relation defined for the Key field

  9. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms

  10. Selection Sort ?? 510324 86 45 30 27 63 96 50 10 ?? 0351 24 86 45 30 27 63 96 5010 ?? 03102486 45 30 27 63 96 50 51 ?? 03102486 45 30 27 63 96 50 51 ?? 03102427 45 30 86 63 96 50 51 ?? 0310242730 45 86 63 96 50 51

  11. Find m such that A[m].Key = min (A[i].Key..A[Size].Key) m := i ; MinKey := A[m].Key; FOR j := i+1 TO Size DO A[j].Key < MinKey Yes m := j; MinKey := A[m].Key Selection Sort FOR i := 1 TO Size-1 DO Swap A[i] and A[m]

  12. PROCEDURESort(VARAARRAYOFItem); VARi,j,m,Size : CARDINAL; MinKey : KeyType; PROCEDURESwap(VAR X,Y : Item); (* Exchange values of X and Y *) ENDSwap; BEGIN Size := High(A); FORi := 1TO SizeDO (* Find m , the index of the smallest key between items [i+1] and [Size] *) Swap(A[i],A[m]) END; (* FOR *) ENDSort; Selection Sort

  13. m := i; MinKey := A[m].Key; FOR j := i + 1 TO Size DO IF A[j].Key < MinKey THEN m := j; MinKey := A[m].Key END (* IF *) END; (* FOR *) Selection SortFind the smallest key in the remaining Array

  14. PROCEDURE Swap(VAR X,Y : Item); VAR Z : Item; BEGIN Z := X; X := Y; Y := Z END Swap; Procedure Swap

  15. - n 1 å n - ( i + 1 ) Number of Comparisons = = i 1 n å i = i = 2 2 n + n - 2 = 2 Selection Sort Performance Number of swaps <= n

  16. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms

  17. Insertion Sort ?? 51 0324 86 45 30 27 63 96 50 10 03 510324 86 45 30 27 63 96 50 10 24 035124 86 45 30 27 63 96 5010 86 0324 5186 45 30 27 63 96 5010 45 0324 518645 30 27 63 96 5010 30 0324 45 518630 27 63 96 5010 27 0324 30 45 518627 63 96 5010

  18. Insertion Sort FOR i := 2 TO Size DO X := A[i] Insert X at the appropriate location between A[1] and A[i], using A[0] as a sentinel A[0] := X; j := i; WHILE X.Key < A[j-1].Key DO A[j] := A[j-1]; DEC( j ); A[j] := X

  19. PROCEDURE Sort(VAR A ARRAY OF Item); VAR i,j,m,Size : CARDINAL; X : Item; BEGIN Size := High(A); FOR i := 2 TO Size DO X := A[i]; A[0] := X; j := i; WHILE X.Key <= A[j-1].Key DO A[j] := A[j-1]; DEC(j) END; A[j] := X END; (* FOR *) END Sort; Insertion Sort

  20. Comparisons minimum : n - 1 average : (n 2+ n - 2 ) / 4 maximum : (n 2- n ) / 2 - 1 Moves minimum : 2 ( n - 1 ) average : ( n 2 - 9 n - 10 ) / 4 maximum : ( n 2 + 3 n - 4 ) / 2 Insertion Sort Performance * * See N. Wirth, Algorithms + data structures = programs, p85

  21. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms

  22. ?? 51 0324 86 45 30 27 63 96 50 10 ?? 0351 24 86 45 30 27 63 96 50 10 ?? 03 24 51 86 45 30 27 63 96 5010 ?? 03 24 51 86 45 30 27 63 96 5010 ?? 03 24 51 45 86 30 27 63 96 5010 ?? 03 24 51 45 30 86 27 63 96 5010 ?? 03 24 51 45 30 27 86 63 96 5010 ?? 03 24 51 45 30 27 6386 96 5010 ?? 03 24 51 45 30 27 63 86 96 5010 ?? 03 24 51 45 30 27 63 86 5096 10 ?? 03 24 51 45 30 27 63 86 50 1096 Bubble Sort (1)

  23. Bubble Sort (2) ?? 03 24 51 45 30 27 63 86 50 1096 ?? 03 24 51 45 30 27 63 86 50 1096 ?? 03 24 51 45 30 27 63 86 50 1096 ?? 03 24 45 51 30 27 63 86 50 1096 ?? 03 24 45 30 51 27 63 86 50 1096 ?? 03 24 45 30 2751 63 86 50 1096 ?? 03 24 45 30 27 5163 86 50 1096 ?? 03 24 45 30 27 51 63 86 50 1096 ?? 03 24 45 30 27 51 63 5086 1096 ?? 03 24 45 30 27 51 63 50 10 86 96

  24. Bubble Sort FOR i := 1 TO Size-1 DO Move the largest element among the elements [1] to [Size-I] to the position [Size – i + 1] FOR j := 1 TO Size - I DO A[j].Key > A[j+1].Key Swap(A[j],A[j+1])

  25. PROCEDURE Sort(VAR A ARRAY OF Item); VAR i,j,Size : CARDINAL; BEGIN Size := High(A); FOR i := 1 TO Size-1 DO FOR j := 1 TO Size-i DO IF A[j].Key >A[j+1].Key THEN Swap(A[j],A[j+1]) END (* IF *) END (* FOR j *) END; (* FOR i *) END Sort; Bubble Sort

  26. - n 1 å Number of Comparisons = i = i 1 2 2 n n - - n n = 2 2 Number of swaps <= Bubble Sort Performance Rem : It is relatively easy to improve the average performance by detecting when the array is entirely sorted but worst case performance remains poor.

  27. PROCEDURE Sort(VAR A ARRAY OF Item); VAR i,j,Size : CARDINAL; InOrder: BOOLEAN; BEGIN Size := High(A); i := 0; REPEAT Inorder := TRUE; i := i + 1; FOR j := 1 TO Size - i DO IF A[j].Key >A[j+1].Key THEN Swap(A[j],A[j+1]); InOrder := FALSE; END (* IF *) END (* FOR j *) UNTIL InOrder OR (i = Size-1); END Sort; Bubble Sort

  28. Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms

  29. Quicksort Partition array A into two parts A1 and A2 in such a way that all keys in A1 are < Pivot and all keys in A2 are > Pivot (Pivot has a value close to the median of key values) More than 1 element in A1 ? Yes Apply QuickSort to array A1 More than 1 element in A2 ? Yes Apply QuickSort to array A2

  30. n n/2 n/2 n/4 n/4 n/4 n/4 n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 Quicksort Performance To sort an array with n elements 1 2 3 Number of partition levels =

  31. Number of comparisons per level To partition an array with n elements : n To partition m arrays with each n/m elements : n Number of partition levels Average length at level k : n / 2 k Levels needed to reach length = 1 : log2 n Total number of comparisons to sort n elements : C = n log2 n Quicksort Performance

  32. Array with 106 elements Processor performing 106 comparisons per second Quicksort C = n log2 n = 106 log2 106 = 20 106 time = 10-6 . 20 106 = 20 s. Selection sort C = ( n2 + n - 1 ) / 2 = ( 1012 + 106 - 1 ) / 2 ~ 5. 1011 time = 10-6 . 5 1011 = 5 105 s ~ 5 days !!! Quicksort PerformanceExample

  33. Quick Sort ?? 51 0324 86 45 30 27 63 96 50 10 ?? 10 0324 27 30 45 86 63 96 50 51 ?? 10 03 2427 3045 86 63 96 50 51 ?? 0310 2427 30 45 86 63 96 50 51 ?? 0310 2427 3045 86 63 96 50 51 ?? 0310 2427 3045 515096 63 86 ?? 0310 2427 3045505196 63 86 ?? 0310 2427 3045 505196 63 86

  34. Quicksort Algorithm (1) Pivot := A[(first+last)DIV2].Key Partition array A into two parts A1 and A2 in such a way that all keys in A1 are < Pivot and all keys in A2 are > Pivot More than 1 element in A1 ? Yes Apply QuickSort to array A1 More than 1 element in A2 ? Yes Apply QuickSort to array A2

  35. Quicksort Algorithm (2) 51 0324 86 45 30 27 63 96 50 10 i j i := first; j := last; WHILE A[i].Key < Pivot INC(i) WHILE A[j].Key > Pivot DEC(j) i < j Yes Swap (A[i],A[j]); INC(i); DEC(j); UNTIL i > j

  36. Quicksort Algorithm (3) 10 0324 86 45 30 27 63 96 50 51 i j i := first; j := last; WHILE A[i].Key < Pivot INC(i) WHILE A[j].Key > Pivot DEC(j) i < j Yes Swap (A[i],A[j]); INC(i); DEC(j); UNTIL i > j

  37. Quicksort Algorithm (4) 10 0324 27 45 30 86 63 96 50 51 i j i := first; j := last; WHILE A[i].Key < Pivot INC(i) WHILE A[j].Key > Pivot DEC(j) i < j Yes Swap (A[i],A[j]); INC(i); DEC(j); UNTIL i > j

  38. Quicksort Algorithm (5) 10 0324 27 30 45 86 63 96 50 51 j i i := first; j := last; WHILE A[i].Key < Pivot INC(i) WHILE A[j].Key > Pivot DEC(j) i < j Yes Swap (A[i],A[j]); INC(i); DEC(j); UNTIL i > j

  39. Partition array A into two parts A1 and A2 in such a way that all keys in A1 are < Pivot and all keys in A2 are > Pivot (Pivot has a value close to the median of key values) More than Min elements in A1 ? Yes Apply QuickSort to array A1 More than Min elements in A2 ? Yes Apply QuickSort to array A2 Optimized Quicksort Apply Insertion Sort to entire array A

More Related