Java Quick Sort quickSort(String[] strings, int begin, int length)

Here you can find the source of quickSort(String[] strings, int begin, int length)

Description

This method sorts lexicographically the strings.

License

Open Source License

Parameter

Parameter Description
strings holds the list with strings for sorting and indexing.

Declaration

static void quickSort(String[] strings, int begin, int length) 

Method Source Code

//package com.java2s;
/**/*from   w w  w  . j a  v  a2s  .c  o m*/
 * Copyright (c) 1997, 2015 by ProSyst Software GmbH and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * This method sorts lexicographically the strings.
     *
     * @param strings holds the list with strings for sorting and indexing.
     */
    static void quickSort(String[] strings, int begin, int length) {
        int i, j, leftLength, rightLength, t;
        String x;
        while (length >= 3) {
            t = length - 1;
            j = t + begin;
            i = (t >> 1) + begin;
            sort3(strings, begin, i, j);
            if (length == 3) {
                return;
            }
            x = strings[i];
            i = begin + 1;
            j--;
            do {
                while (strings[i].compareTo(x) < 0) {
                    i++;
                }
                while (strings[j].compareTo(x) > 0) {
                    j--;
                }
                if (i < j) {
                    swap(strings, i, j);
                } else {
                    if (i == j) {
                        i++;
                        j--;
                    }
                    break;
                }
            } while (++i <= --j);
            leftLength = (j - begin) + 1;
            rightLength = (begin - i) + length;
            if (leftLength < rightLength) {
                if (leftLength > 1) {
                    quickSort(strings, begin, leftLength);
                }
                begin = i;
                length = rightLength;
            } else {
                if (rightLength > 1) {
                    quickSort(strings, i, rightLength);
                }
                length = leftLength;
            }
        }
        if (length == 2 && strings[begin].compareTo(strings[begin + 1]) > 0) {
            swap(strings, begin, begin + 1);
        }
    }

    /**
     * Auxiliary method for sorting lexicographically the strings at the positions x, y and z.
     *
     * @param a represents the array with the strings for sorting.
     * @param x position of the first string.
     * @param y position of the second string.
     * @param z position of the third string.
     */
    private static void sort3(String[] a, int x, int y, int z) {
        if (a[x].compareTo(a[y]) > 0) {
            if (a[x].compareTo(a[z]) > 0) {
                if (a[y].compareTo(a[z]) > 0) {
                    swap(a, x, z);
                } else {
                    swap3(a, x, y, z);
                }
            } else {
                swap(a, x, y);
            }
        } else if (a[x].compareTo(a[z]) > 0) {
            swap3(a, x, z, y);
        } else if (a[y].compareTo(a[z]) > 0) {
            swap(a, y, z);
        }
    }

    /**
     * Auxiliary method for sorting lexicographically the strings. Shuffling strings on positions x and y, as the string
     * at the position x, goes to the position y, the string at the position y, goes to the position x.
     *
     * @param a represents the array with the strings for sorting.
     * @param x position of the first string.
     * @param y position of the second string.
     */
    private static void swap(String[] a, int x, int y) {
        String t = a[x];
        a[x] = a[y];
        a[y] = t;
    }

    /**
     * Auxiliary method for sorting lexicographically the strings. Shuffling strings on positions x, y and z, as the
     * string
     * at the position x, goes to the position z, the string at the position y, goes to the position x and the string
     * at the position z, goes to the position y.
     *
     * @param a represents the array with the strings for sorting.
     * @param x position of the first string.
     * @param y position of the second string.
     * @param z position of the third string.
     */
    private static void swap3(String[] a, int x, int y, int z) {
        String t = a[x];
        a[x] = a[y];
        a[y] = a[z];
        a[z] = t;
    }
}

Related

  1. quickSort(int[] x)
  2. quickSort(short[] fireZoneInfo, int left, int right)
  3. quicksort(String a[], int lo0, int hi0)
  4. quickSort(String a[], int lo0, int hi0)
  5. quickSort(String[] str, int low, int high)
  6. quickSort(T[] array, int[] index, int left, int right)
  7. quickSort1(double array[], int low, int n)
  8. quickSort1(int target[], int fromIndex, int length, int[] coSort)
  9. quickSort2(int target[], int fromIndex, int length, int[] coSort)