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

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

Description

quick Sort

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*//  w w w  . j  a  va2 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.
 */

public class Main {
    private static void quickSort(int[] array, int lo0, int hi0) {
        int lo = lo0;
        int hi = hi0;
        int mid;

        if (hi0 > lo0) {
            mid = array[(lo0 + hi0) / 2];

            while (lo <= hi) {
                while ((lo < hi0) && (array[lo] < mid))
                    ++lo;
                while ((hi > lo0) && (array[hi] > mid))
                    --hi;

                if (lo <= hi) {
                    swap(array, lo, hi);
                    ++lo;
                    --hi;
                }
            }

            if (lo0 < hi)
                quickSort(array, lo0, hi);
            if (lo < hi0)
                quickSort(array, lo, hi0);
        }
    }

    public static void swap(final Object[] array, int pos) {
        swap(array, pos, pos + 1);
    }

    public static void swap(final Object[] array, int pos, boolean forward) {
        int pos2 = (forward) ? pos + 1 : pos - 1;
        swap(array, pos, pos2);
    }

    public static void swap(final Object[] array, int pos, int pos2) {
        if (pos == pos2)
            return;

        Object temp = array[pos];
        array[pos] = array[pos2];
        array[pos2] = temp;
    }

    public static void swap(final int[] array, int pos, int pos2) {
        if (pos == pos2)
            return;

        int temp = array[pos];
        array[pos] = array[pos2];
        array[pos2] = temp;
    }
}

Related

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