Java Quick Sort quickSort( double[] array, int[] index, int left, int right)

Here you can find the source of quickSort( double[] array, int[] index, int left, int right)

Description

Implements quicksort with median-of-three method and explicit sort for problems of size three or less.

License

Open Source License

Parameter

Parameter Description
array the array of doubles to be sorted
index the index into the array of doubles
left the first index of the subset to be sorted
right the last index of the subset to be sorted

Declaration





private static void quickSort( double[] array,  int[] index, int left, int right) 

Method Source Code

//package com.java2s;
/*//from  w  w w  .  ja  v a2 s  . c o 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 3 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, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Implements quicksort with median-of-three method and explicit sort for
     * problems of size three or less. 
     *
     * @param array the array of doubles to be sorted
     * @param index the index into the array of doubles
     * @param left the first index of the subset to be sorted
     * @param right the last index of the subset to be sorted
     */
    //@ requires 0 <= first && first <= right && right < array.length;
    //@ requires (\forall int i; 0 <= i && i < index.length; 0 <= index[i] && index[i] < array.length);
    //@ requires array != index;
    //  assignable index;
    private static void quickSort(/*@non_null@*/ double[] array, /*@non_null@*/ int[] index, int left, int right) {

        int diff = right - left;

        switch (diff) {
        case 0:

            // No need to do anything
            return;
        case 1:

            // Swap two elements if necessary
            conditionalSwap(array, index, left, right);
            return;
        case 2:

            // Just need to sort three elements
            conditionalSwap(array, index, left, left + 1);
            conditionalSwap(array, index, left, right);
            conditionalSwap(array, index, left + 1, right);
            return;
        default:

            // Establish pivot
            int pivotLocation = sortLeftRightAndCenter(array, index, left, right);

            // Move pivot to the right, partition, and restore pivot
            swap(index, pivotLocation, right - 1);
            int center = partition(array, index, left, right, array[index[right - 1]]);
            swap(index, center, right - 1);

            // Sort recursively
            quickSort(array, index, left, center - 1);
            quickSort(array, index, center + 1, right);
        }
    }

    /**
     * Implements quicksort according to Manber's "Introduction to
     * Algorithms".
     *
     * @param array the array of integers to be sorted
     * @param index the index into the array of integers
     * @param left the first index of the subset to be sorted
     * @param right the last index of the subset to be sorted
     */
    //@ requires 0 <= first && first <= right && right < array.length;
    //@ requires (\forall int i; 0 <= i && i < index.length; 0 <= index[i] && index[i] < array.length);
    //@ requires array != index;
    //  assignable index;
    private static void quickSort(/*@non_null@*/ int[] array, /*@non_null@*/ int[] index, int left, int right) {

        if (left < right) {
            int middle = partition(array, index, left, right);
            quickSort(array, index, left, middle);
            quickSort(array, index, middle + 1, right);
        }
    }

    /**
     * Conditional swap for quick sort.
     */
    private static void conditionalSwap(double[] array, int[] index, int left, int right) {

        if (array[index[left]] > array[index[right]]) {
            int help = index[left];
            index[left] = index[right];
            index[right] = help;
        }
    }

    /**
     * Sorts left, right, and center elements only, returns resulting center as pivot.
     */
    private static int sortLeftRightAndCenter(double[] array, int[] index, int l, int r) {

        int c = (l + r) / 2;
        conditionalSwap(array, index, l, c);
        conditionalSwap(array, index, l, r);
        conditionalSwap(array, index, c, r);
        return c;
    }

    /**
     * Swaps two elements in the given integer array.
     */
    private static void swap(int[] index, int l, int r) {

        int help = index[l];
        index[l] = index[r];
        index[r] = help;
    }

    /**
     * Partitions the instances around a pivot. Used by quicksort and
     * kthSmallestValue.
     *
     * @param array the array of doubles to be sorted
     * @param index the index into the array of doubles
     * @param l the first index of the subset 
     * @param r the last index of the subset 
     *
     * @return the index of the middle element
     */
    private static int partition(double[] array, int[] index, int l, int r, double pivot) {

        r--;
        while (true) {
            while ((array[index[++l]] < pivot))
                ;
            while ((array[index[--r]] > pivot))
                ;
            if (l >= r) {
                return l;
            }
            swap(index, l, r);
        }
    }

    /**
     * Partitions the instances around a pivot. Used by quicksort and
     * kthSmallestValue.
     *
     * @param array the array of integers to be sorted
     * @param index the index into the array of integers
     * @param l the first index of the subset 
     * @param r the last index of the subset 
     *
     * @return the index of the middle element
     */
    private static int partition(int[] array, int[] index, int l, int r) {

        double pivot = array[index[(l + r) / 2]];
        int help;

        while (l < r) {
            while ((array[index[l]] < pivot) && (l < r)) {
                l++;
            }
            while ((array[index[r]] > pivot) && (l < r)) {
                r--;
            }
            if (l < r) {
                help = index[l];
                index[l] = index[r];
                index[r] = help;
                l++;
                r--;
            }
        }
        if ((l == r) && (array[index[r]] > pivot)) {
            r--;
        }

        return r;
    }
}

Related

  1. quickSort( double[] array, int[] index, int left, int right)
  2. quickSort( float[] array, int[] index, int left, int right)
  3. quicksort(Comparable[] a)
  4. quicksort(double lista1[], int lista2[], int izq, int der)