Fundamentals of Computer Science II (CSC-152 97F)

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

Outline of Class 32: Bailey's Iterative Quicksort

• Miscellaneous
• Exceptions, Clarified

Miscellaneous

• Happy Halloween!
• Assignment 4 is now graded. Sorry for the delay.
• Any questions on assignment 5?
• Assignment 6 is almost ready. It will be due next Friday. It should be ready this afternoon, perhaps around 4pm. Basically, you'll be
• sorting, using your integer lists; and
• implementing a puzzle solver with both stacks and queues.
• Start reading chapters 10 and 11 in Bailey for next week. We won't spend much time on chapter 10, but it makes good background reading.

Exceptions, Clarified

• There are still some problems with exceptions. We'll consider
• Motivation
• Throwing/Raising
• Handling
• Note that this section was added after class, because of some in-class discussion. It will be completed later today.

Exceptions, Motivated

• Why are there exceptions? To provide a clear error-handling system for Java.

Handling Exceptions

• There are two standard ways (with some variations).
• You can deal with the error yourself.
• You can pass the error on to your calling method.

Bailey's Linear Structure Examples

• There seems to have been some confusion about the exmaples in chapter eight of Java Structures. We'll spend some time going over the ones that seem confusing.

Iterative Quicksort

• As we've seen earlier, many programmers dislike recursion or feel that it's too inefficient. What are the solutions? Writing tail-recursive methods, which can run as efficiently as iterative methods, or simply write iterative methods.
• However, some algorithms are difficult to write iteratively without some extra support. QuickSort is one of those algorithms.
• Recall that quick sort looks something like:
// determine a pivot to use to partition the array into small and
// large parts
...
// shuffle the array into large and small parts
...
// determine the position of the pivot within the array
...
// sort the left half of the array (recursive call)
...
// sort the right half of the array (recursive call)

• Among the reasons that this is difficult to implement recursively is that we have to recursive calls that can't really be joined.
• Bailey asks us to look at the function calls that are made in recursive quicksort and simulate those directly, using a stack to store function calls in progress.
• This is what the computer actually does.
• I'll try a related, but somewhat different tactic: we'll use a stack to store the ranges (or subranges) that need to be sorted.
/**
Sort a vector using the legendary quicksort algorithm which partitions the
vector into small and large parts and then sorts each part.
pre: nonempty vector
post: the vector is sorted (all the original elements are still there
and for all 0 <= i < size()-2, data[i] <= data[i+1])
*/
public static void quickSortIterative(Vector data) {
Range r;		// The current range in the vector that we're sorting
Object pivot; // A value used to partition the vector
int pivloc;	// The location of the pivot
Stack todo;	// A list of the ranges left to sort
// Initially, we need to sort the whole Vector
todo.push(new Range(0,data.size()-1));
// As long as there are ranges left to sort, sort them (or start
// working on them).
while (!todo.empty()) {
r = pop(todo);
// Base case: Size one or less.  Already sorted, so go on to the
//   next thing remaining to sort.
if (r.size() <= 1) next;
// Determine a pivot to use to partition the array into small and
//   large parts
pivot = pickPivot(data,Range);
// Partition the array into large and small parts and
//   determine the position of the pivot within the array
pivloc = partition(data,Range,pivot);
// Note the we half to sort the left half of the array
todo.push(new Range(r.lower(),pivloc-1));
// Note the we half to sort the right half of the array
todo.push(new Range(pivloc+1,r.upper()));
} // while
// That's it, we're done
} // quickSortIterative

Solving Puzzles

• We've done a quick generalization of both the shuffling coins and the maze solving problems.
• There is a Puzzle object that supports
• reset() reset the puzzle to its initial state
• legalMoves() list the legal moves
• makeMove(Move m) make a legal move
• How do we solve it?
• Our first attempt, repeatedly picking a random legal move, isn't guaranteed to work.
• You can get loops.
• Take-home "assignment": think about better solutions.

Outlines: prev next

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.