QuickSort (Java)

De My Limbic Wiki
Aller à : navigation, rechercher
 1 int partition(int arr[], int left, int right){
 2       int i = left, j = right;
 3       int tmp;
 4       int pivot = arr[(left + right) / 2];  
 5 
 6       while (i <= j) {
 7             while (arr[i] < pivot)
 8                   i++;
 9             while (arr[j] > pivot)
10                   j--;
11             if (i <= j) {
12                   tmp = arr[i];
13                   arr[i] = arr[j];
14                   arr[j] = tmp;
15                   i++;
16                   j--;
17             }
18       }
19       return i;
20 }
21 
22 void quickSort(int arr[], int left, int right) {
23       int index = partition(arr, left, right);
24       if (left < index - 1)
25             quickSort(arr, left, index - 1);
26       if (index < right)
27             quickSort(arr, index, right);
28 }