Java Array Sort quickSort(int[] arr, int startIndex, int endIndex)

Here you can find the source of quickSort(int[] arr, int startIndex, int endIndex)

Description

quick Sort

License

Open Source License

Declaration

public static void quickSort(int[] arr, int startIndex, int endIndex) 

Method Source Code


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

import java.util.Arrays;

public class Main {
    public static void quickSort(int[] arr, int startIndex, int endIndex) {
        System.out.println(Arrays.toString(arr));

        int pivotIndex = (startIndex + endIndex) / 2;

        int i = startIndex;
        int j = endIndex;

        while (!(i > j)) {
            // travel from LHS (i)
            while (arr[i] < arr[pivotIndex])
                i++;/*w w w . j  a  v  a  2s . co  m*/

            // travel from RHS (j)
            while (arr[j] > arr[pivotIndex])
                j--;

            System.out.printf("%d %d\n", i, j);

            // swap the numbers
            if (i <= j) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
                i++;
                j--;
                System.out.println(Arrays.toString(arr));
            }
        }

        // divide 2 chunks of array
        // [startIndex .. j] and [i .. endIndex]
        if (startIndex < j) {
            System.out.printf("Dividing %d-%d", startIndex, j);
            quickSort(arr, startIndex, j);
        }

        if (i < endIndex) {
            System.out.printf("Dividing %d-%d", i, endIndex);
            quickSort(arr, i, endIndex);
        }

    }
}

Related

  1. getMedianIndex(float[] sorted, float value)
  2. getNewSortedIntArray(int[] codePointArray)
  3. getSortedDistinct(int[] values)
  4. intArrayToShortArraySorted(int[] source)
  5. multiQuickSort(int[]... arrays)
  6. radixSort(int[] vs)
  7. selectionSort(int[] arr)
  8. sort( final Item[] values, final Item[] auxiliary, final int first, final int last)
  9. sort( float[] array)