chapter 2.9 sorting arrays. sort algorithms a set of records is given each record is identified by a...
TRANSCRIPT
Chapter 2.9
Sorting Arrays
Sort Algorithms
• 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
Key Preparation
• 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.
Alphabetical KeyPROCEDURE 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;
Sort Algorithms
• Key Preparation• Array sorting
– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort
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
Sort Algorithms
• Key Preparation• Array sorting
– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort
Selection Sort
?? 51 03 24 86 45 30 27 63 96 50 10
?? 03 51 24 86 45 30 27 63 96 50 10
?? 03 10 24 86 45 30 27 63 96 50 51
?? 03 10 24 86 45 30 27 63 96 50 51
?? 03 10 24 27 45 30 86 63 96 50 51
?? 03 10 24 27 30 45 86 63 96 50 51
Selection Sort
FOR i := 1 TO Size-1 DO
Find m such that A[m].Key = min (A[i].Key..A[Size].Key)
Swap A[i] and A[m]
m := i ; MinKey := A[m].Key;FOR j := i+1 TO Size DO
m := j;MinKey := A[m].Key
A[j].Key < MinKeyYes
Selection SortPROCEDURE Sort(VAR A ARRAY OF Item);
VAR i,j,m,Size : CARDINAL; MinKey : KeyType;PROCEDURE Swap(VAR X,Y : Item);
(* Exchange values of X and Y *)END Swap;
BEGINSize := High(A);FOR i := 1 TO Size DO
(* Find m , the index of the smallest key between items [i+1] and [Size] *)
Swap(A[i],A[m])END; (* FOR *)
END Sort;
Selection SortFind the smallest key in the remaining
Array
m := i;MinKey := A[m].Key;FOR j := i + 1 TO Size DO
IF A[j].Key < MinKey THENm := j;MinKey := A[m].Key
END (* IF *)END; (* FOR *)
Procedure Swap
PROCEDURE Swap(VAR X,Y : Item);VAR Z : Item;
BEGINZ := X;X := Y;Y := Z
END Swap;
Selection Sort Performance
n i( )1i
n
1
1
in
i2
n n 2 22
Number of Comparisons =
=
=
Number of swaps <= n
Sort Algorithms
• Key Preparation• Array sorting
– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort
Insertion Sort
03 51 03 24 86 45 30 27 63 96 50 10
24 03 51 24 86 45 30 27 63 96 50 10
86 03 24 51 86 45 30 27 63 96 50 10
?? 51 03 24 86 45 30 27 63 96 50 10
45 03 24 51 86 45 30 27 63 96 50 10
30 03 24 45 51 86 30 27 63 96 50 10
27 03 24 30 45 51 86 27 63 96 50 10
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
Insertion SortPROCEDURE Sort(VAR A ARRAY OF Item);
VAR i,j,m,Size : CARDINAL; X : Item;
BEGINSize := 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 Performance *
• 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
* See N. Wirth, Algorithms + data structures = programs, p85
Sort Algorithms
• Key Preparation• Array sorting
– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort
Bubble Sort (1)
?? 03 51 24 86 45 30 27 63 96 50 10?? 03 24 51 86 45 30 27 63 96 50 10?? 03 24 51 86 45 30 27 63 96 50 10
?? 51 03 24 86 45 30 27 63 96 50 10
?? 03 24 51 45 86 30 27 63 96 50 10?? 03 24 51 45 30 86 27 63 96 50 10?? 03 24 51 45 30 27 86 63 96 50 10?? 03 24 51 45 30 27 63 86 96 50 10?? 03 24 51 45 30 27 63 86 96 50 10?? 03 24 51 45 30 27 63 86 50 96 10?? 03 24 51 45 30 27 63 86 50 10 96
Bubble Sort (2)
?? 03 24 51 45 30 27 63 86 50 10 96?? 03 24 51 45 30 27 63 86 50 10 96?? 03 24 45 51 30 27 63 86 50 10 96
?? 03 24 51 45 30 27 63 86 50 10 96
?? 03 24 45 30 51 27 63 86 50 10 96?? 03 24 45 30 27 51 63 86 50 10 96?? 03 24 45 30 27 51 63 86 50 10 96?? 03 24 45 30 27 51 63 86 50 10 96?? 03 24 45 30 27 51 63 50 86 10 96?? 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 theelements [1] to [Size-I] to theposition [Size – i + 1]
FOR j := 1 TO Size - I DO
A[j].Key > A[j+1].Key
Swap(A[j],A[j+1])
Bubble SortPROCEDURE 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 Performance
ii
n
1
1
n n2
2
Number of Comparisons =
=
Number of swaps <= n n2
2
Rem : It is relatively easy to improve the average performance by detecting when the array is entirely sorted but worst case performance remains poor.
Bubble SortPROCEDURE 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;
Sort Algorithms
• Key Preparation• Array sorting
– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort
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 ?
Apply QuickSort to array A1
More than 1 element in A2 ?
Apply QuickSort to array A2
Yes
Yes
Quicksort Performance
To sort an array with n elements
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
1
2
3
Number of partition levels =
Quicksort Performance
• 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
Example• 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 !!!
Quick Sort
?? 10 03 24 27 30 45 86 63 96 50 51
?? 10 03 24 27 30 45 86 63 96 50 51
?? 03 10 24 27 30 45 86 63 96 50 51
?? 51 03 24 86 45 30 27 63 96 50 10
?? 03 10 24 27 30 45 51 50 96 63 86
?? 03 10 24 27 30 45 86 63 96 50 51
?? 03 10 24 27 30 45 50 51 96 63 86
?? 03 10 24 27 30 45 50 51 96 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 ?
Apply QuickSort to array A1
More than 1 element in A2 ?
Apply QuickSort to array A2
Yes
Yes
Quicksort Algorithm (2)
i := first; j := last;
UNTIL i > j
WHILE A[i].Key < Pivot
INC(i)
WHILE A[j].Key > Pivot
DEC(j)
Swap (A[i],A[j]); INC(i); DEC(j);
i < jYes
51 03 24 86 45 30 27 63 96 50 10
i j
Quicksort Algorithm (3)
i := first; j := last;
UNTIL i > j
WHILE A[i].Key < Pivot
INC(i)
WHILE A[j].Key > Pivot
DEC(j)
Swap (A[i],A[j]); INC(i); DEC(j);
i < jYes
10 03 24 86 45 30 27 63 96 50 51
i j
Quicksort Algorithm (4)
i := first; j := last;
UNTIL i > j
WHILE A[i].Key < Pivot
INC(i)
WHILE A[j].Key > Pivot
DEC(j)
Swap (A[i],A[j]); INC(i); DEC(j);
i < jYes
10 03 24 27 45 30 86 63 96 50 51
i j
Quicksort Algorithm (5)
i := first; j := last;
UNTIL i > j
WHILE A[i].Key < Pivot
INC(i)
WHILE A[j].Key > Pivot
DEC(j)
Swap (A[i],A[j]); INC(i); DEC(j);
i < jYes
10 03 24 27 30 45 86 63 96 50 51
ij
Optimized 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 Min elements in A1 ?
Apply QuickSort to array A1
More than Min elements in A2 ?
Apply QuickSort to array A2
Yes
Yes
Apply Insertion Sort to entire array A