[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [Tutorial] [API]
Back to Project Discussion: Specifications. On to Some Sorting Algorithms.
Held Monday, March 6, 2000
Overview
Today we begin our investigations of a key problem in computing: that of sorting. Sorting algorithms turn a collection (typically an array) of elements into a collection that is ordered (most typically, from smallest to largest).
Notes
Contents
Summary
/**
* 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 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()
Tuesday, 18 January 2000
Monday, 6 March 2000
Tuesday, 7 March 2000
Back to Project Discussion: Specifications. On to Some Sorting Algorithms.
[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [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/2000S/Outlines/outline.25.html
Source text last modified Tue Mar 7 09:50:39 2000.
This page generated on Tue Mar 7 09:54:17 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu