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

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

Description

quick Sort

License

Open Source License

Declaration

private static <T extends Comparable<? super T>> void quickSort(T[] array, int[] index, int left, int right) 

Method Source Code

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

public class Main {
    private static <T extends Comparable<? super T>> void quickSort(T[] array, 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);
        }//  ww  w . j  ava 2  s .c o m
    }

    private static void quickSort(/* @non_null@ */double[] 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);
        }
    }

    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);
        }
    }

    private static <T extends Comparable<? super T>> int partition(T[] array, int[] index, int l, int r) {
        T pivot = array[index[(l + r) / 2]];
        int help;
        while (l < r) {
            while (array[index[l]].compareTo(pivot) < 0 && l < r) {
                l++;
            }
            while (array[index[r]].compareTo(pivot) > 0 && l < r) {
                r--;
            }
            if (l < r) {
                help = index[l];
                index[l] = index[r];
                index[r] = help;
                l++;
                r--;
            }
        }
        if (l == r && array[index[r]].compareTo(pivot) > 0) {
            r--;
        }
        return r;
    }

    private static int partition(double[] 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;
    }

    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(short[] fireZoneInfo, int left, int right)
  2. quicksort(String a[], int lo0, int hi0)
  3. quickSort(String a[], int lo0, int hi0)
  4. quickSort(String[] str, int low, int high)
  5. quickSort(String[] strings, int begin, int length)
  6. quickSort1(double array[], int low, int n)
  7. quickSort1(int target[], int fromIndex, int length, int[] coSort)
  8. quickSort2(int target[], int fromIndex, int length, int[] coSort)
  9. quickSortInt(int[] data, int[] dataIdx, int len)