390 likes | 531 Views
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
E N D
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 Quicksort Sort Algorithms
Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms
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
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;
Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms
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
Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms
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
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]
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
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
PROCEDURE Swap(VAR X,Y : Item); VAR Z : Item; BEGIN Z := X; X := Y; Y := Z END Swap; Procedure Swap
- 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
Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms
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
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
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
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
Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms
?? 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)
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
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])
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
- 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.
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
Key Preparation Array sorting Straight Selection Sort Straight Insertion Sort Bubble Sort Quicksort Sort Algorithms
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
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 =
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
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
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
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
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
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
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
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
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