001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.ecoinformatics.seek.querybuilder;
031
032/**
033 * @author Rod Spears
034 * 
035 *         TODO To change the template for this generated type comment go to
036 *         Window - Preferences - Java - Code Generation - Code and Comments
037 */
038public class QuickSort {
039
040        /**
041         * Sorts an Object array of items and uses toString to do String compares
042         * for collation
043         * 
044         * @param aList
045         *            The array of items
046         * @param aLeft
047         *            left index
048         * @param aRight
049         *            the right index
050         */
051        public static void doSort(Object aList[], int aLeft, int aRight) {
052
053                if (aRight > aLeft) {
054                        int i = aLeft - 1;
055                        int j = aRight;
056
057                        while (true) {
058                                while (aList[++i].toString()
059                                                .compareTo(aList[aRight].toString()) < 0)
060                                        ;
061                                while (j > 0)
062                                        if (aList[--j].toString().compareTo(
063                                                        aList[aRight].toString()) <= 0)
064                                                break; // out of while
065
066                                if (i >= j)
067                                        break;
068
069                                swap(aList, i, j);
070                        }
071
072                        swap(aList, i, aRight);
073
074                        doSort(aList, aLeft, i - 1);
075                        doSort(aList, i + 1, aRight);
076                }
077        }
078
079        /**
080         * Swap two Objects in the array
081         * 
082         * @param aList
083         *            The array of items
084         * @param aInx1
085         *            index to swap
086         * @param aInx2
087         *            index to swap
088         */
089        private static void swap(Object aList[], int aInx1, int aInx2) {
090                Object tmp = aList[aInx1];
091                aList[aInx1] = aList[aInx2];
092                aList[aInx2] = tmp;
093        }
094}