[Current] [News] [Glance] [Search] [Instructions] [Links] [Handouts] [Project] [Outlines] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [EIJ] [API]
Back to More Recursive Algorithms. On to More Efficient Sorting Algorithms.
Held Wednesday, October 11, 2000
Summary
Today we move on to algorithm design for a common problem: Putting the elements of a list or array in "order". This problem is called sorting.
Notes
Overview
FIB.
FIB[i] is nonzero, it contains the nth Fibonacci number.
public long fib(int n) {
if (FIB[n] != 0) {
return FIB[n];
}
else {
FIB[n] = fib(n-1) + fib(n-2);
return FIB[n];
}
} // fib(int)
public static long fib(int n) {
long[] FIB = new long[n+1];
FIB[1] = 1;
FIB[2] = 1;
for (int i = 3; i <= n; i++) {
FIB[i] = FIB[i-1] + FIB[i-2];
}
return FIB[n];
} // fib(int)
import IncomparableException;
/**
* Objects used to compare other objects. Based on version 1.2
* of the old Comparable.java by Sam Rebelsky.
*
* @author Samuel A. Rebelsky
* @version 1.0 of October 2000
*/
public interface Ordering {
/**
* Determine if the first element should precede the
* second. Throws an exception if the two elements cannot
* be compared.
*/
public boolean precedes(Object first, Object second)
throws IncomparableException;
/**
* Determine if the first element is equal to the second. Throws
* an exception if the two elements cannot be compared.
*/
public boolean equals(Object first, Object second)
throws IncomparableException;
} // interface Ordering
Comparable and
Comparator interfaces that we might instead use.
import java.util.Comparator;
import IncomparableException;
/**
* Things that you can tell to sort themselves.
*
* @author Samuel A. Rebelsky
* @version 1.2 of October 2000
*/
public interface Sortable {
/**
* Sort the contents of the thing using an ordering to determine
* the relative position of pairs of 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 IncomprableException
* if there are pairs of elements that cannot be compared.
* (Yes, I know that Comparators throw ClassCastExceptions,
* but I believe that's an atrocious idea.
*/
public void sort(Comparator compare)
throws IncomparableException;
} // interface Sortable
/**
* A objects of values which can be indexed by integers from 0 to size()-1.
* Initially, a wrapper class for Java's built-in arrays, now partially
* extended to support subarrays.
*
* @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[])
/**
* Build a new array which holds the Integers that correspond
* to the values given in the array.
*/
public Array(int[] values) {
objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
objects[i] = new Integer(values[i]);
} // for
} // Array(int[])
/**
* Build a new subarray which holds the elements in positions
* start .. finish of stuff. Modifications to the subarray should
* affect the original array and vice versa.
*/
public Array(Array stuff, int start, int finish) {
// STUB. Write this.
} // Array(stuff, start, finish)
// +-----------+-----------------------------------------------
// | 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
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
|
Unsorted Sorted
Open the input file
Open a temporary file for the "more sorted" version
Let largestSoFar = the first element in the input file
While (elements remain in the input file)
Let nextElement = the next element in the input file
If nextElement < largestSoFar then
Write nextElement to the temporary file
Else
Write largestSoFar to the temporary file
Set largestSoFar to nextElement
// We've read the whole file (N elements), but only written
// N-1 elements. Write the last one.
Write largestSoFar to the temporary file
Close the input file
Close the temporary file
Replace the input file with the temporary file
Wednesday, 23 August 2000
Thursday, 24 August 2000
Wednesday, 11 October 2000
Back to More Recursive Algorithms. On to More Efficient Sorting Algorithms.
[Current] [News] [Glance] [Search] [Instructions] [Links] [Handouts] [Project] [Outlines] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [EIJ] [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.cs.grinnell.edu/~rebelsky/Courses/CS152/2000F/Outlines/outline.28.html
Source text last modified Wed Oct 25 10:05:37 2000.
This page generated on Fri Oct 27 08:20:01 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu