temp
/**
Sort a vector, returning a sorted version of the vector.
pre: The elements in the vector are comparable.
post: Returns a sorted version of the vector (defined elsewhere).
post: The original vector is not changed.
*/
public static VectorOfComparable mergeSort(VectorOfComparable v) {
int middle; // Index of middle element
// Base case: vector of size 0 or 1
if (v.size() <= 1) {
return v.clone(); // Cloned, so that it is safe to modify
// the returned vector
} // if
// Recursive case: split and merge
middle = v.size() / 2; // Integer division gives integer
return merge(mergeSort(v.subVector(0,middle));
mergeSort(v.subVector(middle+1,v.size())));
} // mergeSort
/**
Merge two sorted vectors into a single sorted vector.
pre: Both vectors are sorted
pre: Elements in both vectors are comparable
post: The returned vector is sorted, and contains all the
elements of the two vectors
post: The two arguments are not changed
*/
public static VectorOfComparable merge(VOC left, VOC right) {
// Variables
Vector result = new VectorOfComparable(left.size()+right.size());
int left_index=0; // Index into left vector
int right_index=0; // Index into right vector
int index=0; // Index into result vector
// As long both vectors have elements, copy the smaller one.
while ((left_index<left.size()) && (right_index<right.size())) {
if(left[left_index].smaller(right[right_index])) {
result[index++] = left[left_index++];
} // first element in left subvector is smaller
else {
result[index++] = right[right_index++];
} // first element in right subvector is smaller
} // while
// Copy any remaining parts of each vector.
while(left_index<left.size()) {
result[index++] = left[left_index++];
}
while(right_index<right.size()) {
result[index++] = right[right_index++];
}
// That's it
return result;
} // merge
/**
Sort a subvector using quick sort.
pre: All elements are comparable.
pre: 0 <= lb <= ub < size()
post: The vector is sorted.
*/
public void quickSort(int lb, int ub) {
// Variables
Comparable pivot; // The pivot used to split the vector
int mid; // The position of the pivot
// Base case: size one
if (lb == ub) return;
// Pick a pivot. Often, this is the second element of the
// vector (for some reason, it works better than the first).
// More clever selection techniques might also exist.
pivot = selectPivot(lb,ub);
// Determine the position of the pivot
mid = split(pivot, lb, ub);
// Recurse
if (mid-1>lb) quickSort(lb,mid-1);
if (mid+1<ub) quickSort(mid+1,ub);
} // quickSort
/**
Partition a vector into two subvectors: those less than or
equal to a particular element (the pivot), and those greater
than the pivot.
returns: The index of the pivot (mid) if it's in the vector.
returns: the index of the largest element smaller than
pivot, if it's not.
pre: lb <= ub
pre: the elements in the vector are comparable
post: for all 0 <= i <= mid, mid < j < size()
elementAt(i) <= elementAt(j)
*/
public int partition(Comparable pivot, int lb, int ub) {
// Keep going until we reach the middle
while(lb < ub) {
// If we have a small enough element on the left, advance
// the lower-bound.
if (elementAt(lb).lessEqual(pivot)) ++lb;
// If we have a large enough element on the right, advance
// the upper bound. For safety, we don't advance both in the
// same round.
else if (pivot.less(elementAt(ub))) --ub;
// If necessary, swap elements in the wrong part of the
// vector
if (pivot.less(elementAt(lb)) &&
elementAt(ub).less(pivot)) {
swap(lb,ub);
}
} // while
// lb = ub, so we can return
return lb;
} // split
partition method actually works (that it partitions
correctly and that it terminates).
selectionSort, and would take time
O(n^2).
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 Wed Oct 8 08:43:13 1997.
This page generated on Wed Nov 5 12:38:35 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu