Java Quick Sort quickSort(int[] array, int[] index, int lo0, int hi0)

Here you can find the source of quickSort(int[] array, int[] index, int lo0, int hi0)

Description

Implements quicksort for an array of indices.

License

Open Source License

Parameter

Parameter Description
array the array of integers to be sorted
index the index which should contain the positions in the sorted array
lo0 the first index of the subset to be sorted
hi0 the last index of the subset to be sorted

Declaration

private static void quickSort(int[] array, int[] index, int lo0, int hi0) 

Method Source Code

//package com.java2s;
/*//from  w w  w . java2s.co  m
 * 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
 * (at your option) 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

public class Main {
    /**
     * Implements quicksort for an array of indices.
     *
     * @param array the array of integers to be sorted
     * @param index the index which should contain the positions in the
     * sorted array
     * @param lo0 the first index of the subset to be sorted
     * @param hi0 the last index of the subset to be sorted
     */
    private static void quickSort(int[] array, int[] index, int lo0, int hi0) {

        int lo = lo0;
        int hi = hi0;
        int mid;
        int help;

        if (hi0 > lo0) {

            // Arbitrarily establishing partition element as the midpoint of
            // the array.
            mid = array[index[(lo0 + hi0) / 2]];

            // loop through the array until indices cross
            while (lo <= hi) {

                // find the first element that is greater than or equal to  
                // the partition element starting from the left Index.
                while ((array[index[lo]] < mid) && (lo < hi0)) {
                    ++lo;
                }

                // find an element that is smaller than or equal to 
                // the partition element starting from the right Index.
                while ((array[index[hi]] > mid) && (hi > lo0)) {
                    --hi;
                }

                // if the indexes have not crossed, swap
                if (lo <= hi) {
                    help = index[lo];
                    index[lo] = index[hi];
                    index[hi] = help;
                    ++lo;
                    --hi;
                }
            }

            // If the right index has not reached the left side of array
            // must now sort the left partition.
            if (lo0 < hi) {
                quickSort(array, index, lo0, hi);
            }

            // If the left index has not reached the right side of array
            // must now sort the right partition.
            if (lo < hi0) {
                quickSort(array, index, lo, hi0);
            }
        }
    }

    /**
     * Implements unsafe quicksort for an array of indices.
     *
     * @param array the array of doubles to be sorted
     * @param index the index which should contain the positions in the
     * sorted array
     * @param lo0 the first index of the subset to be sorted
     * @param hi0 the last index of the subset to be sorted
     */
    private static void quickSort(double[] array, int[] index, int lo0, int hi0) {

        int lo = lo0;
        int hi = hi0;
        double mid;
        int help;

        if (hi0 > lo0) {

            // Arbitrarily establishing partition element as the midpoint of
            // the array.
            mid = array[index[(lo0 + hi0) / 2]];

            // loop through the array until indices cross
            while (lo <= hi) {

                // find the first element that is greater than or equal to  
                // the partition element starting from the left Index.
                while ((array[index[lo]] < mid) && (lo < hi0)) {
                    ++lo;
                }

                // find an element that is smaller than or equal to 
                // the partition element starting from the right Index.
                while ((array[index[hi]] > mid) && (hi > lo0)) {
                    --hi;
                }

                // if the indexes have not crossed, swap
                if (lo <= hi) {
                    help = index[lo];
                    index[lo] = index[hi];
                    index[hi] = help;
                    ++lo;
                    --hi;
                }
            }

            // If the right index has not reached the left side of array
            // must now sort the left partition.
            if (lo0 < hi) {
                quickSort(array, index, lo0, hi);
            }

            // If the left index has not reached the right side of array
            // must now sort the right partition.
            if (lo < hi0) {
                quickSort(array, index, lo, hi0);
            }
        }
    }
}

Related

  1. quickSort(int[] arr, int left, int right)
  2. quickSort(int[] array)
  3. quicksort(int[] array)
  4. quickSort(int[] array)
  5. quickSort(int[] array, int lo0, int hi0)
  6. quicksort(int[] source)
  7. quickSort(int[] source)
  8. quickSort(int[] target, int[] coSort)
  9. quickSort(int[] x)