cs1110 11 november 2008 sorting: insertion sort, selection sort, quick sort

12
1 CS1110 11 November 2008 Sorting: insertion sort, selection sort, quick sort Do exercises on pp. 311-312 to get familiar with concepts and develop skill. Practice in DrJava! Test your methods! What do these words have in common? Banana Dresser Grammar Potato Revive Uneven Assess

Upload: merlin

Post on 14-Jan-2016

40 views

Category:

Documents


0 download

DESCRIPTION

CS1110 11 November 2008 Sorting: insertion sort, selection sort, quick sort. Do exercises on pp. 311-312 to get familiar with concepts and develop skill. Practice in DrJava! Test your methods!. What do these words have in common? Banana Dresser Grammar Potato Revive Uneven Assess. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

1

CS1110 11 November 2008Sorting: insertion sort, selection sort, quick sort

Do exercises on pp. 311-312 to get familiar with concepts and develop skill. Practice in DrJava! Test your methods!

What do these words have in

common?

BananaDresser

GrammarPotato

ReviveUnevenAssess

Page 2: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

2

Reversing array segment b[h..k]

reversed

h j k

post: b

b not reversed

h k

pre:

1 2 3 4 5 6 7 8 9 9 9 9 bh k

change:

into 9 9 9 9 8 7 6 5 4 3 2 1bh k

Page 3: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

3

Sorting:

?

0 npre: b sorted

0 npost: b

sorted ?

0 i n

insertion sort inv: b

for (int i= 0; i < n; i= i+1) {

}

“sorted” means in ascending order

2 4 4 6 6 7 50 i

2 4 4 5 6 6 70 i

Push b[i] down into its sorted position in b[0..i];

Iteration i makes up to i swaps.In worst case, number of swaps needed is 0 + 1 + 2 + 3 + … (n-1) = (n-1)*n / 2.

Called an “n-squared”, or n2, algorithm.

b[0..i-1]: i elements

in worst case:

Iteration 0: 0 swapsIteration 1: 1 swapIteration 2: 2 swaps…

Page 4: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

4

?0 npre: b sorted

0 npost: b

Add property to invariant: first segment contains smaller values.

≤ b[i..], sorted ≥ b[0..i-1], ? 0 i ninvariant: b

selection sort

sorted ? 0 i ninvariant: b

insertion sort

for (int i= 0; i < n; i= i+1) {

}

2 4 4 6 6 8 9 9 7 8 9i n

2 4 4 6 6 7 9 9 8 8 9i n

7int j= index of min of b[i..n-1];

Swap b[j] and b[i];

Also an “n-squared”, or n2, algorithm.

Page 5: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

5

Quicksort/** Sort b[h..k] */public static void qsort(int[] b, int h, int k) {

}

if (b[h..k] has fewer than 2 elements) return;

j= partition(b, h, k);

<= x x >= x h j k

post: b

x ? h k

pre: b

int j= partition(b, h, k);

// b[h..j–1] <= b[j] < b[j+1..k]

// Sort b[h..j–1] and b[j+1..k]

qsort(b, h, j–1);

qsort(b, j+1, k);

To sort array of size n. e.g. 215

Worst case: n2 e.g. 230

Average case: n log n. e.g. 15 * 215

215 = 32768

Page 6: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

6

Tony Hoare,

in 1968

Quicksort author

Tony Hoare,

in 2007

in Germany

Thought of Quicksort in ~1958. Tried to explain it to a colleague, but couldn’t.Few months later: he saw a draft of the definition of the language Algol 58 –later turned into Algol 60. It had recursion.He went and explained Quicksort to his colleague, using recursion, who now understood it.

Page 7: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

The NATO Software Engineering Conferenceshomepages.cs.ncl.ac.uk/brian.randell/NATO/

7-11 Oct 1968, Garmisch, Germany 27-31 Oct 1969, Rome, Italy

Download Proceedings, whichhave transcripts of discussions.See photographs.

Software crisis:Academic and industrial people. Admitted for first time that they did not know how to develop software efficiently and effectively.

Page 8: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

8

Software Engineering, 1968

Next 10-15 years: intense period of research of software engineering, language design, proving programs correct, etc.

Page 9: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

9Software Engineering, 1968

Page 10: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

10

Editors of the1968 Proceedings

1968

Peter Naur

~2000

1968

Brian Randell

2000?

Page 11: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

11Edsger W. Dijkstra Niklaus Wirth Tony Hoare

Beards

The reason why some people grow aggressive tufts of facial hairis that they do not like to show the chin that isn't there.

Piet Hein

Page 12: CS1110    11 November 2008 Sorting: insertion sort, selection sort, quick sort

12

Sir Tony Hoare, Fritz Bauer

Quicksort author My advisor

Pioneer in computing in the 50's, 60's. Developer of the historical computing section of the Deutsches Museum

Marienplatz, Munich

Oct 2007, 40th anniversary of CS Dept in the Technical University Munich. Hoare and I gave invited lectures.