Download presentation

Presentation is loading. Please wait.

Published byKacie Duxbury Modified about 1 year ago

1
Chapter 2.9 Sorting Arrays

2
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.

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

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

5
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.

6
Alphabetical Key PROCEDURE MakeKey(VAR Names, Key: ARRAY OF CHAR); VARn,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
Sort Algorithms Key Preparation Array sorting –Straight Selection Sort –Straight Insertion Sort –Bubble Sort –Quicksort

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
Sort Algorithms Key Preparation Array sorting –Straight Selection Sort –Straight Insertion Sort –Bubble Sort –Quicksort

10
Selection Sort ?? ?? ?? ?? ??

11
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 < MinKey Yes

12
Selection Sort PROCEDURE 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; BEGIN Size := 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;

13
Selection Sort Find 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 THEN m := j; MinKey := A[m].Key END (* IF *) END; (* FOR *)

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

15
Selection Sort Performance n i ()1 i n 1 1 i n i 2 nn Number of Comparisons = = = Number of swaps <= n

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

17
Insertion Sort ??

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
Insertion Sort 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;

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

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

22
Bubble Sort (1) ?? ?? ?? ?? ?? ?? ?? ?? ??

23
Bubble Sort (2) ?? ?? ?? ?? ?? ?? ??

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
Bubble Sort 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;

26
Bubble Sort Performance i i n 1 1 nn 2 2 Number of Comparisons = = Number of swaps <= nn 2 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.

27
Bubble Sort 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;

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

29
Quicksort Partition array A into two parts A1 and A2 in such a way that all keys in A1 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

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

31
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 : log 2 n – Total number of comparisons to sort n elements : C = n log 2 n

32
Quicksort Performance Example Array with 10 6 elements Processor performing 10 6 comparisons per second Quicksort C = n log 2 n = 10 6 log = time = = 20 s. Selection sort C = ( n 2 + n - 1 ) / 2 = ( ) / 2 ~ time = = s ~ 5 days !!!

33
Quick Sort ?? ?? ?? ?? ?? ??

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 More than 1 element in A1 ? Apply QuickSort to array A1 More than 1 element in A2 ? Apply QuickSort to array A2 Yes

35
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 < j Yes ij

36
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 < j Yes ij

37
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 < j Yes ij

38
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 < j Yes ij

39
Optimized Quicksort Partition array A into two parts A1 and A2 in such a way that all keys in A1 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 Apply Insertion Sort to entire array A

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google