// Sort.java
// By Ned Etcode
// Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.

/** Object subclass containing static sorting methods.
  * @note 1.0 upperCaseStrings checks for null element in [] objects
  */
public class Sort
{
    private Sort()
    {
        throw new Error("All methods on Sort are static, do not call new Sort().");
    }

    /** Sorts a n array of numbers. It also operates
      * on a parallel array of Objects--as elements of the number
      * array are swapped, corresponding elements in the Object array are
      * swapped as well.
      */
    public static void sort(double array[], Object other[], int begin, int count, boolean ascending)
    {
		if (count <= 1)
			return;

		quickSort(array, other, begin, begin + count - 1, ascending);
    }
    
    private static void quickSort(double array[], Object other[], int left, int right, boolean ascending)
    {
        int i, j;
        double pivot;
        double tmpD;
        Object tmpO;

        if (array.length <= 1)
            return;

        i = left;
        j = right;

        pivot = array[(left + right) / 2];

        do
        {
            if (ascending)
            {
                while (i < right && pivot > array[i])
                    i++;

                while (j > left && pivot < array[j])
                    j--;
            }
            else
            {
                while (i < right && pivot < array[j])
                    i++;

                while (j > left && pivot > array[i])
                    j--;
            }

            if (i < j)
            {
                tmpD = array[i];
                array[i] = array[j];
                array[j] = tmpD;

                if (other != null)
                {
                    tmpO = other[i];
                    other[i] = other[j];
                    other[j] = tmpO;
                }
            }

            if (i <= j)
            {
                i++;
                j--;
            }
        }
        while (i <= j);

        if (left < j)
            quickSort(array, other, left, j, ascending);

        if (i < right)
            quickSort(array, other, i, right, ascending);
    }
}
