class QuickSort { public static void main( String[] args ) { int[] array = { 8, 6, 9, 13, 1, 17, 6, 19, 2, 18, 16 }; qsort( array ); } public static void qsort( int[] array ) { sortSubArray( array, 0, array.length-1 ); } private static void sortSubArray( int[] array, int l, int u ) { if( l >= u ) return; // find the pivot value // use the array[l] point as the pivot final int pivot = array[l]; int m = l; // scan the sub-array from l to u, // partitioning it to 2 groups. // move values < p to positions <=m in the array // keep values >= p to positions >m in the array for( int i=l+1; i <= u; i++ ) { if( array[i] < pivot ) { m++; swap( array, m, i ); } } // move the pivot to the correct position swap( array, l, m ); // recursively sort the partitions sortSubArray( array, l, m-1 ); sortSubArray( array, m+1, u ); } private static void swap( int[] array, int i, int j ) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } }