Class 37: Exam 2

Back to Arrays. On to Designing Objects.

Held: Monday, 7 April 2003

Summary: Today we go over some of the key issues from exam 2.

Related Pages:

Assignments:

Notes:

• I rearranged the syllabus so that we can talk about object-oriented design this week and about exam 2 today.
• Reminder: We meet in Saints' Rest tomorrow.
• Expect exam 3 on Friday.
• Are there questions on homework 3?

Overview:

• Quicksort.
• From Strings to Values.

Quicksorting Arrays

• Note: As I reviewed this, it looks like I didn't screw up.
• You still all get full credit.

Some Background

• How does the standard Quicksort procedure work?
• It identifies a pivot value (often the first value in the list)
• It splits the list into two parts: Those smaller than the pivot and those larger than the pivot.
• We'll need to do more or less the same thing for arrays, except that we won't build new arrays.
• How do we separate the array into parts? We'll do what we did with `binary-search`: use indices to keep track of the lower and upper bounds of the parts.

The Partition Code

• This procedure is clearly (or perhaps not so clearly) intended to do the split into two parts part.
• What are the four parameters?
```define partition!
(let ((swap! (lambda (vec i j)
(let ((tmp (vector-ref vec i)))
(vector-set! vec i (vector-ref vec j))
(vector-set! vec j tmp)))))
(lambda (vec lower upper can-come-before?)
(if (= lower upper)
lower
(begin
(let kernel! ((l lower)
(u upper))
(cond
((= u l)
(begin
(swap! vec lower u)
u))
((not (can-come-before? (vector-ref vec u)
(vector-ref vec lower)))
(kernel! l (- u 1)))
((can-come-before? (vector-ref vec l)
(vector-ref vec lower))
(kernel! (+ l 1) u))
(else (begin
(swap! vec l u)
(kernel! l u))))))))))
```

History

Thursday, 15 January 2003 [Samuel A. Rebelsky]

• Created as a mostly-blank outline.

Sunday, 6 April 2003 [Samuel A. Rebelsky]

• Filled in the details.

Back to Arrays. On to Designing Objects.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:20:55 2003.
The source to the document was last modified on Sun Apr 6 23:20:49 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.37.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu