( , !):
EDIT: . 2 , .
public static void main (String[] args) throws java.lang.Exception
{
int b[] = {10, 9, 8, 7, 7, 7, 7, 3, 2, 1};
sort(b,0,b.length-1);
System.out.println(Arrays.toString(b));
}
static void sort(int a[], int left, int right) {
if (right > left){
int i=left, j=right, tmp;
int v = a[right];
do {
while(a[i]<v)
i++;
while(a[j]>v)
j--;
if( i <= j){
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i++;
j--;
}
} while(i <= j);
if(left < j) sort(a,left,j);
if(i < right) sort(a,i,right);
}
}
- IDEOne
, , I/J, , , .
, , ( ), , , , , j, 0 ( ), 1.:)
, 2 , , . , , . , , j, , . , j , , j .
RosettaCode:
function quicksort(array)
if length(array) > 1
pivot := select any element of array
left := first index of array
right := last index of array
while left โค right
while array[left] < pivot
left := left + 1
while array[right] > pivot
right := right - 1
if left โค right
swap array[left] with array[right]
left := left + 1
right := right - 1
quicksort(array from first index to right)
quicksort(array from left to last index)
: SO
, - oridnary while
:)