Here you can find the source of quicksort(int[] source)
Parameter | Description |
---|---|
source | the array of integers that needs to be sorted. |
public static void quicksort(int[] source)
//package com.java2s; public class Main { /**/*from w w w. ja va2 s .c om*/ * Sorts this array of integers according to the Quicksort algorithm. After * calling this method this array is sorted in ascending order with the * smallest integer taking position 0 in the array. * <p> * This implementation is based on the quicksort algorithm as described in * <code>Data Structures In Java</code> by Thomas A. Standish, Chapter 10, * ISBN 0-201-30564-X. * * @param source the array of integers that needs to be sorted. */ public static void quicksort(int[] source) { quicksort(source, 0, source.length - 1); } /** * Sort a subarray of a source array. The subarray is specified by its start * and end index. * * @param source the int array to be sorted * @param left the start index of the subarray * @param right the end index of the subarray */ public static void quicksort(int[] source, int left, int right) { if (right > left) { int index = partition(source, left, right, right); quicksort(source, left, index - 1); quicksort(source, index + 1, right); } } /** * Split a subarray of a source array into two partitions. The left * partition contains elements that have value less than or equal to the * pivot element, the right partition contains the elements that have larger * value. * * @param source the int array whose subarray will be splitted * @param left the start position of the subarray * @param right the end position of the subarray * @param pivotIndex the index of the pivot element inside the array * @return the new index of the pivot element inside the array */ private static int partition(int[] source, int left, int right, int pivotIndex) { int pivot = source[pivotIndex]; source[pivotIndex] = source[right]; source[right] = pivot; int index = left; for (int i = left; i < right; i++) { if (source[i] <= pivot) { int tmp = source[index]; source[index] = source[i]; source[i] = tmp; index++; } } int tmp = source[index]; source[index] = source[right]; source[right] = tmp; return index; } }