quicksort is the fastest sorting algorithm, and here is example of method in Java:
static public void quickSort(int[] arr, int l, int r) {It needs to be called like this:
int left = l; int right = r; int tmp;
int middle = arr[(left + right) / 2];
while (left <= right) {
while (arr[left]<middle) left++;
while (arr[right]>middle) right--;
if (arr[left]>arr[right]) {
tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
}
if (left<r) quickSort(arr, left, r);
if (l<right) quickSort(arr, l, right);
}
public static void main(String args[]) {
int[] array = { 4, 23,65, 76, 99, 12, 90, 34 }
quickSort(array, 0, array.length-1)
}
Комментариев нет:
Отправить комментарий