// 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)
/**
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
Puzzle object that supports
reset() reset the puzzle to its initial state
legalMoves() list the legal moves
makeMove(Move m) make a legal move
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.
Source text last modified Fri Oct 31 12:12:44 1997.
This page generated on Wed Nov 5 12:38:27 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu