com.tffenterprises.util
Class AbstractMergeSort

java.lang.Object
  extended by com.tffenterprises.util.AbstractMergeSort
Direct Known Subclasses:
StringMergeSort

public abstract class AbstractMergeSort
extends java.lang.Object

AbstractMergeSort implements a merge sort, minus the comparison function. Inspired by a Sun example.

Version:
1.0d1 $Date: 2002/10/12 19:59:22 $
Author:
Daniel M. Zimmerman

Field Summary
protected  java.lang.Object[] arrayToSort
          An array of objects to sort.
protected  java.lang.Object[] scratch
          An array of scratch space.
 
Constructor Summary
AbstractMergeSort()
           
 
Method Summary
protected abstract  int compareElementsAt(int firstIndex, int secondIndex)
          The method that must be overridden to provide a comparison function.
protected  void merge(int beginIndex, int middleIndex, int endIndex)
          The method that actually does the merge in merge sort.
 void mergeSort(int beginIndex, int endIndex)
          The recursive method for the merge sort.
 void sort(java.lang.Object[] arrayToSort)
          Sorts (in place) an array of objects.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

arrayToSort

protected java.lang.Object[] arrayToSort
An array of objects to sort.


scratch

protected java.lang.Object[] scratch
An array of scratch space.

Constructor Detail

AbstractMergeSort

public AbstractMergeSort()
Method Detail

sort

public void sort(java.lang.Object[] arrayToSort)
Sorts (in place) an array of objects.

Parameters:
arrayToSort - The array to sort.

mergeSort

public void mergeSort(int beginIndex,
                      int endIndex)
The recursive method for the merge sort.

Parameters:
beginIndex - The index at which to begin sorting.
endIndex - The index at which to end sorting.

merge

protected void merge(int beginIndex,
                     int middleIndex,
                     int endIndex)
The method that actually does the merge in merge sort.

Parameters:
beginIndex - The starting index of the first half to merge.
middleIndex - The ending index of the first half to merge.
endIndex - The ending index of the second half to merge.

compareElementsAt

protected abstract int compareElementsAt(int firstIndex,
                                         int secondIndex)
The method that must be overridden to provide a comparison function.

Parameters:
firstIndex - The index of the first element to compare.
secondIndex - The index of the second element to compare.
Returns:
a negative integer if the element at firstIndex is less than the element at secondIndex, a positive integer if the element at firstIndex is greater than the element at secondIndex, and 0 if the two elements are equal.