chapter 2.9 sorting arrays. sort algorithms a set of records is given each record is identified by a...

39
Chapter 2.9 Sorting Arrays

Upload: kacie-duxbury

Post on 14-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Chapter 2.9

Sorting Arrays

Page 2: 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

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.

Page 3: 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

Sort Algorithms

• Key Preparation• Array sorting

– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort

Page 4: 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

Sort Algorithms

• Key Preparation• Array sorting

– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort

Page 5: 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

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.

Page 6: 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

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;

Page 7: 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

Sort Algorithms

• Key Preparation• Array sorting

– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort

Page 8: 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

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

Page 9: 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

Sort Algorithms

• Key Preparation• Array sorting

– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort

Page 10: 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

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

Page 11: 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

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

Page 12: 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

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;

Page 13: 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

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 *)

Page 14: 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

Procedure Swap

PROCEDURE Swap(VAR X,Y : Item);VAR Z : Item;

BEGINZ := X;X := Y;Y := Z

END Swap;

Page 15: 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

Selection Sort Performance

n i( )1i

n

1

1

in

i2

n n 2 22

Number of Comparisons =

=

=

Number of swaps <= n

Page 16: 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

Sort Algorithms

• Key Preparation• Array sorting

– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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;

Page 20: 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

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

Page 21: 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

Sort Algorithms

• Key Preparation• Array sorting

– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort

Page 22: 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

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

Page 23: 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

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

Page 24: 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

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])

Page 25: 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

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;

Page 26: 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

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.

Page 27: 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

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;

Page 28: 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

Sort Algorithms

• Key Preparation• Array sorting

– Straight Selection Sort– Straight Insertion Sort– Bubble Sort– Quicksort

Page 29: 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

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

Page 30: 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

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 =

Page 31: 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

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

Page 32: 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

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 !!!

Page 33: 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

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

Page 34: 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

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

Page 35: 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

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

Page 36: 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

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

Page 37: 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

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

Page 38: 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

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

Page 39: 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

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