Java Array Sort sortStrings(final String[] strings)

Here you can find the source of sortStrings(final String[] strings)

Description

Quick sort an array of Strings.

License

Open Source License

Parameter

Parameter Description
string Strings to be sorted

Declaration


public static void sortStrings(final String[] strings) 

Method Source Code

//package com.java2s;
/*//from w  w  w . j  a va2 s .co m
 * 20:25:20 20/05/99
 *
 * The Java Shell: Utilities.
 * (C)1999 Romain Guy, Osvaldo Pinali Doederlein.
 *
 * LICENSE
 * =======
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * CHANGES
 * =======
 * 1.0.8 - Filled the listRoots method                   (Romain & Osvaldo)
 * 1.0.7 - Several bug fixes in constructPath            (Romain)
 * 1.0.6 - Split JDK1.1/1.2                              (Osvaldo)
 * 1.0.5 - Important bug fix in constructPath(String)    (Romain)
 * 1.0.4 - Added getSize(Enumeration)                    (Osvaldo)
 * 1.0.3 - Changed sortStrings bubble-sort algorithm to  (Romain)
 *         quick-sort (James Gosling)
 * 1.0.2 - Fixed two little bug in constructPath(String) (Romain)
 * 1.0.1 - Added listFiles(String[], boolean)            (Romain)
 *       - Removed unecessary createWhiteSpace(int)      (Romain)
 *       - Modified getWildCardMatches(String, boolean)  (Romain)
 *       - Slighty improved javadoc comments             (Romain)
 * 1.0.0 - Initial release.                              (Romain & Osvaldo)
 *
 * LINKS
 * =====
 * Contact: mailto@osvaldo.visionnaire.com.br
 * Site #1: http://www.geocities.com/ResearchTriangle/Node/2005/
 * Site #2: http://student.vub.ac.be/~opinalid/
 */

public class Main {
    /**
     * Quick sort an array of Strings.
     *
     * @param string
     *                Strings to be sorted
     */

    public static void sortStrings(final String[] strings) {
        sortStrings(strings, 0, strings.length - 1);
    }

    /**
     * Quick sort an array of Strings.
     *
     * @param a
     *                Strings to be sorted
     * @param lo0
     *                Lower bound
     * @param hi0
     *                Higher bound
     */

    public static void sortStrings(final String a[], final int lo0, final int hi0) {
        int lo = lo0;
        int hi = hi0;
        String mid;

        if (hi0 > lo0) {
            mid = a[(lo0 + hi0) / 2];

            while (lo <= hi) {
                while (lo < hi0 && a[lo].compareTo(mid) < 0) {
                    ++lo;
                }

                while (hi > lo0 && a[hi].compareTo(mid) > 0) {
                    --hi;
                }

                if (lo <= hi) {
                    swap(a, lo, hi);
                    ++lo;
                    --hi;
                }
            }

            if (lo0 < hi) {
                sortStrings(a, lo0, hi);
            }

            if (lo < hi0) {
                sortStrings(a, lo, hi0);
            }
        }
    }

    /**
     * Swaps two Strings.
     *
     * @param a
     *                The array to be swapped
     * @param i
     *                First String index
     * @param j
     *                Second String index
     */

    public static void swap(final String a[], final int i, final int j) {
        String T;
        T = a[i];
        a[i] = a[j];
        a[j] = T;
    }
}

Related

  1. sortStringArray(String array[], boolean inplace)
  2. sortStringArray(String[] array)
  3. sortStringArray(String[] array)
  4. sortStringArray(String[] source)
  5. sortStringArrayAlphabetically(String[] strings)
  6. sortStrings(String[] strings)
  7. sortStrings(String[] strings)
  8. sortStrings(String[] strings)
  9. sortSubFiles(String[] p_subFiles)