2 views

Original Title: 21ElementarySorts.pdf

Uploaded by bui ton

- USC_Algorithms_Lecture_Slides.pdf
- Data Structure Mcqs
- Introduction_to_algorithms_A_creative_approach (1).pdf
- microverse-syllabus-2019
- Status Messages
- Ext Sort
- Advanced Data Structures
- median finding algorithm
- Divide Conquer
- 05divide and Conquer 2x2 1
- 23 Quick Sort
- 24PriorityQueues.pdf
- 22Mergesort.pdf
- 23DemoPartitioning.pdf
- 23DemoQuickSelect.pdf
- LabVIEW State Machines
- its in the cards student worksheet
- us_oracle-hcm-core-tables-erd-new.pdf
- FLUX-WEAKENING
- Earth Fact Sheet_NASA

You are on page 1of 63

rules of the game

selection sort

insertion sort

Algorithms

shellsort

F O U R T H E D I T I O N

shuffling

http://algs4.cs.princeton.edu

2.1 E LEMENTARY S ORTS

rules of the game

selection sort

insertion sort

Algorithms

shellsort

shuffling

http://algs4.cs.princeton.edu

Sorting problem

Chen 3 A 991-878-4944 308 Blair

Andrews 3 A 664-480-0023 097 Little

3

Sorting applications

contacts

FedEx packages

4

Sample sort client 1

Ex 1. Sort random real numbers in ascending order.

{ 0.08614716385210452

public static void main(String[] args) 0.09054270895414829

{ 0.10708746304898642

int N = Integer.parseInt(args[0]); 0.21166190071646818

Double[] a = new Double[N]; 0.363292849257276

for (int i = 0; i < N; i++) 0.460954145685913

a[i] = StdRandom.uniform(); 0.5340026311350087

Insertion.sort(a); 0.7216129793703496

for (int i = 0; i < N; i++) 0.9003500354411443

StdOut.println(a[i]); 0.9293994908845686

}

}

5

Sample sort client 2

Ex 2. Sort strings in alphabetical order.

{

public static void main(String[] args)

{

String[] a = StdIn.readAllStrings();

Insertion.sort(a);

for (int i = 0; i < a.length; i++)

StdOut.println(a[i]);

}

}

% more words3.txt

bed bug dad yet zoo ... all bad yes

all bad bed bug dad ... yes yet zoo

[suppressing newlines]

6

Sample sort client 3

Ex 3. Sort the files in a given directory by filename.

Insertion.class

public class FileSorter Insertion.java

{ InsertionX.class

public static void main(String[] args) InsertionX.java

{ Selection.class

File directory = new File(args[0]); Selection.java

File[] files = directory.listFiles(); Shell.class

Insertion.sort(files); Shell.java

for (int i = 0; i < files.length; i++) ShellX.class

StdOut.println(files[i].getName()); ShellX.java

}

}

7

Total order

Goal. Sort any type of data (for which sorting is well defined).

Antisymmetry: if both v w and w v, then v = w.

Transitivity: if both v w and w x, then v x.

Totality: either v w or w v or both.

Ex.

Standard order for natural and real numbers.

Chronological order for dates or times. COS 423 COS 333

COS 226 COS 217

No transitivity. Rock-paper-scissors.

COS 126

No totality. PU course prerequisites.

violates transitivity violates totality

8

Callbacks

Goal. Sort any type of data (for which sorting is well defined).

Q. How can sort() know how to compare data of type Double, String, and

java.io.File without any information about the type of an item's key?

Client passes array of objects to sort() function.

The sort() function calls object's compareTo() method as needed.

Implementing callbacks.

Java: interfaces.

C: function pointers.

C++: class-type functors.

C#: delegates.

Python, Perl, ML, Javascript: first-class functions.

9

Callbacks: roadmap

data-type implementation

client

public class String

implements Comparable<String>

public class StringSorter {

{ ...

public static void main(String[] args) public int compareTo(String b)

{ {

String[] a = StdIn.readAllStrings(); ...

Insertion.sort(a); return -1;

for (int i = 0; i < a.length; i++) ...

StdOut.println(a[i]); return +1;

} ...

} return 0;

}

}

public interface Comparable<Item>

{

{

int N = a.length;

public int compareTo(Item that);

for (int i = 0; i < N; i++)

}

for (int j = i; j > 0; j--)

if (a[j].compareTo(a[j-1]) < 0)

exch(a, j, j-1);

key point: no dependence else break;

on String data type }

10

Comparable API

Defines a total order.

Returns a negative integer, zero, or positive integer

if v is less than, equal to, or greater than w, respectively.

Throws an exception if incompatible types (or either is null).

v w

v w

w v

less than (return -1) equal to (return 0) greater than (return +1)

User-defined comparable types. Implement the Comparable interface.

11

Implementing the Comparable interface

{

private final int month, day, year;

{ to other dates

month = m;

day = d;

year = y;

}

{

if (this.year < that.year ) return -1;

if (this.year > that.year ) return +1;

if (this.month < that.month) return -1;

if (this.month > that.month) return +1;

if (this.day < that.day ) return -1;

if (this.day > that.day ) return +1;

return 0;

}

}

12

2.1 E LEMENTARY S ORTS

rules of the game

selection sort

insertion sort

Algorithms

shellsort

shuffling

http://algs4.cs.princeton.edu

Selection sort demo

Swap a[i] and a[min].

initial

14

Selection sort

Invariants.

No entry to right of is smaller than any entry to the left of .

in final order

15

Two useful sorting abstractions

{ return v.compareTo(w) < 0; }

Exchange. Swap item in array a[] at index i with the one at index j.

{

Comparable swap = a[i];

a[i] = a[j];

a[j] = swap;

}

16

Selection sort inner loop

i++;

in final order

int min = i;

for (int j = i+1; j < N; j++)

if (less(a[j], a[min])) in final order

min = j;

exch(a, i, min);

in final order

17

Selection sort: Java implementation

{

public static void sort(Comparable[] a)

{

int N = a.length;

for (int i = 0; i < N; i++)

{

int min = i;

for (int j = i+1; j < N; j++)

if (less(a[j], a[min]))

min = j;

exch(a, i, min);

}

}

{ /* as before */ }

{ /* as before */ }

}

18

Selection sort: animations

20 random items

algorithm position

in final order

not in final order

http://www.sorting-algorithms.com/selection-sort

19

Selection sort: animations

20 partially-sorted items

algorithm position

in final order

not in final order

http://www.sorting-algorithms.com/selection-sort

20

Selection sort: mathematical analysis

and N exchanges.

a[]

entries in black

i min 0 1 2 3 4 5 6 7 8 9 10 are examined to find

the minimum

S O R T E X A M P L E

0 6 S O R T E X A M P L E

entries in red

1 4 A O R T E X S M P L E are a[min]

2 10 A E R T O X S M P L E

3 9 A E E T O X S M P L R

4 7 A E E L O X S M P T R

5 7 A E E L M X S O P T R

6 8 A E E L M O S X P T R

7 10 A E E L M O P X S T R

8 8 A E E L M O P R S T X

entries in gray are

9 9 A E E L M O P R S T X in final position

10 10 A E E L M O P R S T X

A E E L M O P R S T X

Data movement is minimal. Linear number of exchanges.

21

2.1 E LEMENTARY S ORTS

rules of the game

selection sort

insertion sort

Algorithms

shellsort

shuffling

http://algs4.cs.princeton.edu

Insertion sort demo

23

Insertion sort

Invariants.

Entries to the right of have not yet been seen.

24

Insertion sort inner loop

i++;

in order not yet seen

a[i] with each larger entry to its left.

if (less(a[j], a[j-1]))

exch(a, j, j-1);

else break;

in order not yet seen

25

Insertion sort: Java implementation

{

public static void sort(Comparable[] a)

{

int N = a.length;

for (int i = 0; i < N; i++)

for (int j = i; j > 0; j--)

if (less(a[j], a[j-1]))

exch(a, j, j-1);

else break;

}

{ /* as before */ }

{ /* as before */ }

}

26

Insertion sort: animation

40 random items

algorithm position

in order

not yet seen

http://www.sorting-algorithms.com/insertion-sort

27

Insertion sort: animation

40 reverse-sorted items

algorithm position

in order

not yet seen

http://www.sorting-algorithms.com/insertion-sort

28

Insertion sort: animation

40 partially-sorted items

algorithm position

in order

not yet seen

http://www.sorting-algorithms.com/insertion-sort

29

Insertion sort: mathematical analysis

insertion sort uses ~ N 2 compares and ~ N 2 exchanges on average.

a[]

i j 0 1 2 3 4 5 6 7 8 9 10

S O R T E X A M P L E entries in gray

do not move

1 0 O S R T E X A M P L E

2 1 O R S T E X A M P L E

3 3 O R S T E X A M P L E

4 0 E O R S T X A M P L E entry in red

is a[j]

5 5 E O R S T X A M P L E

6 0 A E O R S T X M P L E

7 2 A E M O R S T X P L E

entries in black

8 4 A E M O P R S T X L E moved one position

9 2 A E L M O P R S T X E right for insertion

10 2 A E E L M O P R S T X

A E E L M O P R S T X

30

Insertion sort: trace

31

Insertion sort: analysis

N 1 compares and 0 exchanges.

A E E L M O P R S T X

insertion sort makes ~ N 2 compares and ~ N 2 exchanges.

X T S R P O M L F E A

32

Insertion sort: partially-sorted arrays

A E E L M O T R X P S

(6 inversions)

Ex 1. A sorted array has 0 inversions.

Ex 2. A subarray of size 10 appended to a sorted subarray of size N.

Pf. Number of exchanges equals the number of inversions.

33

Insertion sort: practical improvements

Eliminates unnecessary data movement.

No longer uses only less() and exch() to access data.

A C H H I M N N P Q X Y K B I N A R Y

Number of compares ~ N lg N .

But still a quadratic number of array accesses.

A C H H I M N N P Q X Y K B I N A R Y

34

2.1 E LEMENTARY S ORTS

rules of the game

selection sort

insertion sort

Algorithms

shellsort

shuffling

http://algs4.cs.princeton.edu

Shellsort overview

Idea. Move entries more than one position at a time by h-sorting the array.

h=4

L E E A M H L E P S O L T S X R

L M P T

E H S S

E L O X

A E L R

h = 13

P H E L L S O R T E X A M S L E

Shellsort. P 1959] h-sort array

[Shell for decreasing sequence

S of values of h.

H L

E E

input S H E LL L S O R T E X A M P L E

13-sort P H E L LL S O R T E X A M S L E

(8 additional files of size 1)

4-sort L E E A M H L E P S O L T S X R

An h-sorted file is h interleaved sorted files

1-sort A E E E H L L L M O P R S S T X

36

h-sorting demo

In iteration i, swap a[i] with each larger entry h positions to its left.

h-sorting

3-sorting an array

M O L E E X A S P R T

E O L M E X A S P R T

E E L M O X A S P R T

E E L M O X A S P R T

A E L E O X M S P R T

A E L E O X M S P R T

A E L E O P M S X R T

A E L E O P M S X R T

A E L E O P M S X R T

A E L E O P M S X R T

Small increments nearly in order. [stay tuned]

38

Shellsort example: increments 7, 3, 1

input 1-sort

S O R T E X A M P L E A E L E O P M S X R T

A E L E O P M S X R T

A E L E O P M S X R T

7-sort

A E E L O P M S X R T

S O R T E X A M P L E A E E L O P M S X R T

M O R T E X A S P L E A E E L O P M S X R T

M O R T E X A S P L E A E E L M O P S X R T

M O L T E X A S P R E A E E L M O P S X R T

M O L E E X A S P R T A E E L M O P S X R T

A E E L M O P R S X T

A E E L M O P R S T X

3-sort

M O L E E X A S P R T

E O L M E X A S P R T

result

E E L M O X A S P R T

E E L M O X A S P R T A E E L M O P R S T X

A E L E O X M S P R T

A E L E O X M S P R T

A E L E O P M S X R T

A E L E O P M S X R T

A E L E O P M S X R T

39

Shellsort: Java implementation

{

public static void sort(Comparable[] a)

{

int N = a.length;

while (h < N/3) h = 3*h + 1; // 1, 4, 13, 40, 121, 364, ... sequence

while (h >= 1)

{ // h-sort the array.

for (int i = h; i < N; i++) insertion sort

{

for (int j = i; j >= h && less(a[j], a[j-h]); j -= h)

exch(a, j, j-h);

}

move to next

h = h/3;

increment

}

}

{ /* as before */ }

private static void exch(Comparable[] a, int i, int j)

{ /* as before */ }

}

40

Shellsort: visual trace

input

40-sorted

13-sorted

4-sorted

result

41

Shellsort: animation

50 random items

algorithm position

h-sorted

current subsequence

http://www.sorting-algorithms.com/shell-sort other elements

42

Shellsort: animation

50 partially-sorted items

algorithm position

h-sorted

current subsequence

http://www.sorting-algorithms.com/shell-sort other elements

43

Shellsort: which increment sequence to use?

No.

Maybe.

OK. Easy to compute.

Good. Tough to beat in empirical studies.

merging of (9 4i) (9 2i) + 1

and 4i (3 2i) + 1

44

Shellsort: intuition

7-sort 3-sort

S O R T E X A M P L E M O L E E X A S P R T

M O R T E X A S P L E E O L M E X A S P R T

M O R T E X A S P L E E E L M O X A S P R T

M O L T E X A S P R E E E L M O X A S P R T

M O L E E X A S P R T A E L E O X M S P R T

A E L E O X M S P R T

A E L E O P M S X R T

A E L E O P M S X R T

A E L E O P M S X R T

A E L E O P M S X R T

still 7-sorted

45

Shellsort: analysis

used by shellsort with the 3x+1 increments is N 3/2.

ordered array using 3x+1 increments is.

46

Why are we interested in shellsort?

R, bzip2, /linux/kernel/groups.c

Useful in practice.

Fast unless array size is huge (used for small subarrays).

Tiny, fixed footprint for code (used in some embedded systems).

Hardware sort prototype. uClibc

Asymptotic growth rate?

Best sequence of increments? open problem: find a better increment sequence

Average-case performance?

47

Elementary sorts summary

selection sort N2 N2 N2

insertion sort N N2 N2

48

2.1 E LEMENTARY S ORTS

rules of the game

selection sort

insertion sort

Algorithms

shellsort

shuffling

http://algs4.cs.princeton.edu

How to shuffle an array

all permutations

equally likely

50

How to shuffle an array

all permutations

equally likely

51

Shuffle sort

Sort the array. useful for shuffling

columns in a spreadsheet

52

Shuffle sort

Sort the array. useful for shuffling

columns in a spreadsheet

53

Shuffle sort

Sort the array. useful for shuffling

columns in a spreadsheet

uniformly at random (and no ties)

54

War story (Microsoft)

ballot screen for users to select browser in Windows 7.

http://www.browserchoice.eu

appeared last

50% of the time

55

War story (Microsoft)

ballot screen for users to select browser in Windows 7.

return a random answer.

Microsoft's

public intimplementation in Javascript that)

compareTo(Browser

{

function

double r RandomSort

= Math.random();

(a,b)

{if (r < 0.5) return -1;

browser comparator

if return

(r > 0.5) (0.5return

- Math.random());

+1;

}return 0; (should implement a total order)

56

Knuth shuffle demo

Swap a[i] and a[r].

57

Knuth shuffle

Swap a[i] and a[r].

uniformly random permutation of the input array in linear time.

assuming integers

uniformly at random

58

Knuth shuffle

Swap a[i] and a[r]. common bug: between 0 and N 1

correct variant: between i and N 1

{

...

public static void shuffle(Object[] a)

{

int N = a.length;

for (int i = 0; i < N; i++)

{

int r = StdRandom.uniform(i + 1); between 0 and i

exch(a, i, r);

}

}

}

59

Broken Knuth shuffle

A. Not uniformly random!

instead of 0 and i

60

War story (online poker)

http://www.datamation.com/entdev/article.php/616221

61

War story (online poker)

for i := 1 to 52 do begin

r := random(51) + 1; between 1 and 51

swap := card[r];

card[r] := card[i];

card[i] := swap;

end;

Bug 1. Random number r never 52 52nd card can't end up in 52nd place.

Bug 2. Shuffle not uniform (should be between 1 and i).

Bug 3. random() uses 32-bit seed 232 possible shuffles.

Bug 4. Seed = milliseconds since midnight 86.4 million shuffles.

The generation

Exploit. of random

After seeing numbers

5 cards andis synchronizing

too important to be left server

with to chance.

clock,

can determine

Robert R.allCoveyou

future cards in real time.

62

War story (online poker)

Use a hardware random-number generator that has passed both

the FIPS 140-2 and the NIST statistical test suites.

Continuously monitor statistic properties:

hardware random-number generators are fragile and fail silently.

Use an unbiased shuffling algorithm.

63

- USC_Algorithms_Lecture_Slides.pdfUploaded byprestonchaps
- Data Structure McqsUploaded byStoriesofsuperheroes
- Introduction_to_algorithms_A_creative_approach (1).pdfUploaded byJudyC.Guevara
- microverse-syllabus-2019Uploaded byrickyx
- Status MessagesUploaded byPratheev S
- Ext SortUploaded bymanuelq9
- Advanced Data StructuresUploaded byNagesh Nadigatla
- median finding algorithmUploaded byvivek upadhyay
- Divide ConquerUploaded by007wasr
- 05divide and Conquer 2x2 1Uploaded bySreeprasad Govindankutty

- LabVIEW State MachinesUploaded bygungun23
- its in the cards student worksheetUploaded byapi-290100812
- us_oracle-hcm-core-tables-erd-new.pdfUploaded byjack_rzm
- FLUX-WEAKENINGUploaded bynikanikolina
- Earth Fact Sheet_NASAUploaded byapi-3704956
- Various CCNA NotesUploaded byAatif Ansari
- TGI0086 (1)Uploaded byÇoktiklananlar Tiklananlar
- Introduction to Lattice QCDUploaded byganzkompliziert
- Rheology_handout.pdfUploaded byPaulo Tércio
- Data Base TechnologyUploaded byشايك يوسف
- Classification Of Protein Based on Composition.pdfUploaded byRidwan Hambali
- Digital Image ProcessingUploaded bynivas28
- Processing for Visual Artists- How to Create Expressive Images and Interactive Art.pdfUploaded byRay Spender
- 1. Benzene. (Review) NEW 15.pdfUploaded byvladimirbenzene
- Basics of hiltiUploaded byHaitham Kayyali
- Least Squares Pursuit for Sparse Signal RecoveryUploaded bynikcleju
- Research Paper on nanotechnologyUploaded byRoboconDK
- H06226Uploaded bykirankirk
- LcdUploaded byAnonymous bjJ8PPZ
- ASTM D 3385 _ 94-2003_Infiltration Rate of Soils in Field Using Double-Ring.pdfUploaded byMartin Daniel Palacios Quevedo
- File HandlingUploaded byranjan_prashant52
- Selecting Thermodynamic Property in HYSYSUploaded bydeltaoperativ
- ch13.pptUploaded byFerdinand Macol
- Sequence and Series Maths RevisionUploaded bybitch
- Question Bank IUploaded byManish Jaiswal
- MainUploaded byNumkang Kang
- hitman studyUploaded bydragon146
- NS - Excercise 04 (1)Uploaded byMikko Saifudin AS
- Demo-Detailed Lesson Plan in MathematicsUploaded byLynskhey Invento Procorato
- udr_200Uploaded byDam