com.photoviewer.client.util
Class QuickSort

java.lang.Object
  extended bycom.photoviewer.client.util.QuickSort

public class QuickSort
extends java.lang.Object

Generic QuickSort (only exists because this code is attempting to be completely Java 1.1 compatible).

Author:
John De Regnaucourt
API:
This class will sort any direct access collection containing any Object type. It is implemented using C.A.R. Hoare's QuickSort alogrithm.
To use: simply instantiate a QuickSort instance and call the sort() method. When instantiating, it is easiest to pass an anonymous class that implements the compare() method. This method compares two objects and returns a negative value when (obj1 < obj2), 0 when (obj1 == obj2), or a positive value when (obj1 > obj2).See example 1.
To use with a collection other than an Object[] or a Vector, see example 2.
============================================================================
Example 1: usage for an Object[] or Vector:
 {
  String[] s = new String[];	// 's' is later filled with content
  Vector v = new Vector();	// 'v' is later filled with content

   ...

  QuickSort q = new QuickSort();

  q.sort(s);		// Sort array of strings
  q.sort(v);		// sort vector of strings
 }

============================================================================
Example 2: To use with a collection that is not a Vector or an Object[]:
{
  SomeCollection m_collator = getMeACollection();
   ...
  QuickSort q = new QuickSort(new QuickSort.Comparator()
  {
      java.text.Collator m_collator = java.text.Collator.getInstance();
      public int compare(Object o1, Object o2)
      {
          Person p1 = (Person) o1;
          Person p2 = (Person) o2;
          return m_collator.compare(p1.getName(), p2.getName());
      }
  });

  q.sort(m_collator, 0, m_collator.itemCount(), new QuickSort.CollectionAccessor()
  {
      public Object getElementAt(Object array, int index)
      {
          SomeCollection c = (SomeCollection) array;
          return c.getItem(index);
      }

      public void setElementAt(Object array, int index, Object value)
      {
          SomeCollection c = (SomeCollection) array;
          c.setItem(index, value);
      }
  });
 }

Nested Class Summary
static interface QuickSort.CollectionAccessor
          Implement this interface and pased it to the sort() method when sorting collections that are not Object[]'s or Vectors.
static interface QuickSort.Comparator
          Implement this inteface and pass it to the QuickSort constructor that takes a Comparator when sorting a collection of items that are not Strings.
 
Constructor Summary
QuickSort()
          Instantiate a QuickSort instance that uses the default String comparator.
QuickSort(QuickSort.Comparator comparator)
          Instantiate a QuickSort instance that used the passed in Comparator.
 
Method Summary
protected  void qsort(java.lang.Object collection, int left, int right)
          This will handle arrays that are already sorted, and arrays with duplicate keys.
 void sort(java.lang.Object collection)
          Sort the passed in Object[] or Vector.
 void sort(java.lang.Object collection, int start, int end)
          Sort the passed range of the passed in Object[] or Vector.
 void sort(java.lang.Object collection, int start, int end, QuickSort.CollectionAccessor col)
          Sort the passed in collection, using the CollectionAccessor interface to access items in the collection.
protected  void swap(java.lang.Object collection, int i, int j)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QuickSort

public QuickSort(QuickSort.Comparator comparator)
Instantiate a QuickSort instance that used the passed in Comparator.

Parameters:
comparator - Comparator that compares two objects.

QuickSort

public QuickSort()
Instantiate a QuickSort instance that uses the default String comparator.

Method Detail

qsort

protected void qsort(java.lang.Object collection,
                     int left,
                     int right)
This will handle arrays that are already sorted, and arrays with duplicate keys.

Parameters:
collection - a collection of java.lang.Objects
left - left boundary of collection partition
right - right boundary of collection partition

swap

protected void swap(java.lang.Object collection,
                    int i,
                    int j)

sort

public void sort(java.lang.Object collection)
Sort the passed in Object[] or Vector.

Parameters:
collection - Object[] or Vector (or derivative of one of those).

sort

public void sort(java.lang.Object collection,
                 int start,
                 int end)
Sort the passed range of the passed in Object[] or Vector.

Parameters:
collection - Object[] or Vector to sort.
start - int begin index of range to sort (0 based)
end - end index of range to sort (0 based)

sort

public void sort(java.lang.Object collection,
                 int start,
                 int end,
                 QuickSort.CollectionAccessor col)
Sort the passed in collection, using the CollectionAccessor interface to access items in the collection. This API allow QuickSort to work on collections that are not Vectors or Object[]'s

Parameters:
collection - CollectionAccessor to sort
start - int begin index of range to sort (0-based)
end - int end index of range to sort (0-based)
col - CollectionAccessor implementation to provide access to a collection that is not a Vector or Object[].