[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to More Efficient Sorting Algorithms. On to Project Discussion.
Held Monday, October 11, 1999
Overview
Notes
Contents
Handouts
Summary
Added after class. You can find a similar discussion in outline 25.
import Comparator;
import IncomparableException;
/**
* Things that you can tell to sort themselves.
*/
public interface Sortable {
/**
* Sort the contents of the thing using a comparator
* to compare elements.
* Pre: The elements can be compared.
* Post: The elements are sorted. Each element is no greater
* the the next element.
* Post: No elements are added or deleted.
*
* @exception IncomparableException
* if there are pairs of elements that cannot be compared.
*/
public void sort(Comparator compare)
throws IncomparableException;
} // Sortable()
IndexedCollection.
Array.
/**
* A objects of values which can be indexed by
* integers from 0 to size(). Basically, a wrapper
* class for Java's built-in arrays.
*
* @author Samuel A. Rebelsky
* @version 1.0 of September 1999
*/
public class Array {
// +--------+--------------------------------------------------
// | Fields |
// +--------+
/** The elements of the objects. */
public Object[] objects;
// +--------------+--------------------------------------------
// | Constructors |
// +--------------+
/**
* Build a new array which holds up to n elements.
* Initially, each element is null.
*/
public Array(int n) {
objects = new Object[n];
} // Array(int)
/**
* Build a new array which holds the specified
* set of elements.
*/
public Array(Object[] elements) {
objects = new Object[elements.length];
for (int i = 0; i < elements.length; ++i) {
objects[i] = elements[i];
}
} // Array(Object[])
// +-----------+-----------------------------------------------
// | Accessors |
// +-----------+
/**
* Get the ith element of the array.
*
* @exception ArrayIndexOutOfBoundsException
* For the obvious reasons.
*/
public Object get(int i) {
return objects[i];
} // get(int)
/**
* Get the number of elements in the array.
*/
public int size() {
return objects.length;
} // size()
// +-----------+-----------------------------------------------
// | Modifiers |
// +-----------+
/**
* Set the ith element of the array.
*
* @exception ArrayIndexOutOfBoundsException
* For the obvious reasons.
*/
public void set(int i, Object value) {
objects[i] = value;
} // set(int, Object)
/**
* Swap the ith and jth elements of the array.
* Included because it's commonly used during sorting.
*/
public void swap(int i, int j) {
Object temp = objects[i];
objects[i] = objects[j];
objects[j] = temp;
} // swap(int,int)
} // class Array
import Sortable;
import Array;
import Comparator;
/**
* Arrays that can sort themselves.
*/
public class SortableArray
extends Array
implements Sortable
{
public void sort(Comparator compare) {
// ...
} // sort(Comparator)
} // class SortableArray
Tuesday, 10 August 1999
Monday, 11 October 1999
Array
class.
Back to More Efficient Sorting Algorithms. On to Project Discussion.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [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/99F/Outlines/outline.27.html
Source text last modified Mon Oct 11 11:49:13 1999.
This page generated on Wed Oct 13 09:43:44 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu