[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Back to A Stamp Problem. On to Discussion of Exam 1.
Held Friday, February 26
Summary
Contents
Handouts
Notes
In essence, dynamic programming calculates the solution to all subproblems. The computation proceeds from the small subproblems to the larger subproblems, storing the answers in a table. The advantage of the method lies in the fact that once a subproblem is solved, the answer is stored and never recalculated.
elementAt references are to ``the current object''.
/**
* Sort all the elements in the array.
* Pre: the elements in the array are comparable using
* a lessEqual method.
* Post: elementAt(i) <= elementAt(i+1) for all 0 <= i < size()-1.
* Post: no element is added to or removed from the subarray.
* Post: no element outside the subarray is affected.
*/
public void selectionSort() {
selectionSort(0,size()-1);
} // selectionSort()
/**
* Sort all the elements in the subarray between lb and ub.
* Pre: 0 <= lb <= ub < size()
* Pre: the elements in the array are comparable using
* a lessEqual method.
* Post: elementAt(lb) <= elementAt(lb+1) <= ... elementAt(ub).
* Post: no element is added to or removed from the subarray.
* Post: no element outside the subarray is affected.
*/
public void selectionSort(int lb, int ub) {
// Variables
int index; // Index of the largest element in subrange
// Base case: one element, so it's sorted. (Don't need to check
// empty subarray because of preconditions.)
if (lb == ub) return;
// Find the index of the largest element in the subrange
index = indexOfLargest(lb,ub);
// Swap that element and the last element
swap(index, ub);
// Sort the rest of the subvector (if there is any)
// Note that we don't have to compare ub-1 to lb, since
// the preconditions and the base case take care of it.
selection_sort(lb,ub-1);
} // selectionSort
/**
* Find the index of the largest element in a subvector
* Pre: 0 <= lb <= ub < size()
* Pre: the elements in the vector are comparable using
* a lessEqual method
* Post: returns I s.t. for all i, lb <= i <= ub,
* elementAt(I) >= elementAt(i)
*/
public int indexOfLargest(int lb, int ub) {
// Variables
int guess; // Current guess as to index of largest
// Make initial guesses
guess = lb;
// Repeatedly improve our guesses until we've looked at
// all the elements
for(int i = lb+1; i <= lb; ++i) {
if (elementAt(guess).lessEqual(elementAt(i))) {
guess = i;
} // if
} // for
// That's it
return guess;
} // index_of_largest
/**
* Sort all the elements in the array using iterative selection sort.
* Pre: the elements in the array are comparable using
* a lessEqual method.
* Post: elementAt(i) <= elementAt(i+1) for all 0 <= i < size()-1.
* Post: no element is added to or removed from the subarray.
* Post: no element outside the subarray is affected.
*/
public void selectionSort() {
// Starting at the top of the array and working your way down
// the array
for (int i = size()-1; i > 0; --i) {
// Put the largest element at the current position.
swap(indexOfLargest(0,i), i);
} // for
} // selectionSort()
/**
* Sort the array.
* Pre: the elements in the are are comparable using lessEqual.
* Post: elementAt(lb) <= elementAt(lb+1) <= ... <= elementAt(ub)
* Post: elements are neither added to nor removed.
*/
public void insertionSort(int lb, int ub)
throws Exception
{
// An object that we're about to insert.
Comparable tmp;
// The correct place for that element in the sorted subarray.
int place;
// Initially, we know that the first element is "sorted" (all
// one element lists are sorted), so we step through the elements
// starting with the second element.
for (int i = 2; i < size(); ++i) {
// Grab the element.
tmp = elementAt(i);
// Clear out the element.
setElementAt(i,null);
// Find the place.
place = findPlace(0,i-1);
// Put the element there.
insertElementAt(i,tmp);
} // for(i)
} // insertionSort(int,int)
findPlace() (which finds the proper
place in the vector to insert the element). Each insertion
requires O(n) steps, and each place determination takes
O(log_2(n)) steps (as long as we can use binary search, so the
running time is O(n*(n+log_2(n)) which is O(n^2).
/**
* Sort a subvector.
* Pre: 0 <= lb <= ub < size()
* Pre: the elements in the vector are comparable using
* a lessEqual method
* Post: elementAt(lb) <= elementAt(lb+1) <= ... <= elementAt(ub)
* Post: elements are neither added to nor removed from the vector.
*/
public void insertionSort(int lb, int ub)
throws Exception
{
// Variables
Comparable tmp; // The object that we'll be inserting
// Base case: one element, so it's sorted
if (lb == ub) return;
// Remember the element at the end, and then remove it
tmp = elementAt(ub);
removeElementAt(ub);
// Sort all but that element
insertionSort(lb,ub-1);
// Insert the element at the proper place. This may throw an
// IncomparableException.
insert(findPlace(lb,ub-1,tmp), tmp);
} // insertionSort
History
Back to A Stamp Problem. On to Discussion of Exam 1.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
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.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Outlines/outline.20.html
Source text last modified Fri Feb 26 09:47:43 1999.
This page generated on Fri Feb 26 09:49:44 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu