Vector-like object, so we don't explicity refer
to the vector.
/**
Sort all the elements in the subvector between lb and ub.
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)
*/
public void selectionSort(int lb, int ub) {
// Variables
int index_of_largest; // ... element in subrange
// The preconditions will be checked in the following
// function call, so we don't check them here
// Base case: one element, so it's sorted
if (lb == ub) {
return;
}
// Find the index of the largest element in the subrange
index_of_largest = indexOfLargest(lb,ub);
// Swap that element and the last element
swap(index_of_largest, 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
// Check the preconditions
Assert.pre(lb <= ub, "nonempty subrange");
Assert.pre(0 <= lb, "reasonable starting point");
Assert.pre(ub < size(), "reasonable ending point");
// 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
Vector)
/**
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)
*/
public void insertionSort(int lb, int ub) {
// Variables
Object tmp; // The object that we'll be inserting
// Check the preconditions
Assert.pre(lb <= ub, "nonempty subrange");
Assert.pre(0 <= lb, "reasonable starting point");
Assert.pre(ub < size(), "reasonable ending point");
// 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
insert(findPlace(lb,ub-1,tmp), tmp);
} // insertionSort
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, so the running time is O(n*(n+log_2(n))
which is O(n).
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 Tue Oct 7 17:13:18 1997.
This page generated on Wed Nov 5 12:38:36 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu