Java Quick Sort quickSort(double[] array, int low, int high)

Here you can find the source of quickSort(double[] array, int low, int high)

Description

Quicksort filter.

License

Open Source License

Parameter

Parameter Description
array The array to sort.
low Ending datasetIndex of the part of the array to sort.

Declaration

public static void quickSort(double[] array, int low, int high) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from w w  w .j  av  a 2s  . co m
     * Quicksort filter. Use low = 0 and high = length - 1 to sort the whole
     * array. From <a href=
     * "http://www.vogella.com/articles/JavaAlgorithmsQuicksort/article.html"
     * >http://www.vogella.com/articles/JavaAlgorithmsQuicksort/article.html<a>
     * 
     * @param array The array to sort.
     * @param low Starting datasetIndex of the part of the array to sort.
     * @param low Ending datasetIndex of the part of the array to sort.
     */
    public static void quickSort(double[] array, int low, int high) {
        int i = low, j = high;
        // Get the pivot element from the middle of the list
        double pivot = array[low + (high - low) / 2];

        // Divide into two lists
        double exchange;
        while (i <= j) {
            // If the current value from the left list is smaller then the pivot
            // element then get the next element from the left list
            while (array[i] < pivot) {
                i++;
            }
            // If the current value from the right list is larger then the pivot
            // element then get the next element from the right list
            while (array[j] > pivot) {
                j--;
            }

            // If we have found a values in the left list which is larger then
            // the pivot element and if we have found a value in the right list
            // which is smaller then the pivot element then we exchange the
            // values.
            // As we are done we can increase i and j
            if (i <= j) {
                exchange = array[i];
                array[i] = array[j];
                array[j] = exchange;
                i++;
                j--;
            }
        }
        // Recursion
        if (low < j)
            quickSort(array, low, j);
        if (i < high)
            quickSort(array, i, high);
    }
}

Related

  1. quickSort( double[] array, int[] index, int left, int right)
  2. quickSort( double[] array, int[] index, int left, int right)
  3. quickSort( float[] array, int[] index, int left, int right)
  4. quicksort(Comparable[] a)
  5. quicksort(double lista1[], int lista2[], int izq, int der)
  6. quickSort(double[] array, int[] idx, int from, int to)
  7. quicksort(double[] main, int[] index)
  8. quicksort(final double[] array, final int[] index)
  9. quicksort(final long[] data, final int left, final int right)