1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import java.util.*; public class QuickSort {
public static void qSort(int[] arr, int head, int tail) { if (head >= tail || arr == null || arr.length <= 1) { return; } int i = head, j = tail, pivot = arr[(head + tail) / 2]; while (i <= j) { while (arr[i] < pivot) { ++i; } while (arr[j] > pivot) { --j; } if (i < j) { int t = arr[i]; arr[i] = arr[j]; arr[j] = t; ++i; --j; } else if (i == j) { ++i; } } qSort(arr, head, j); qSort(arr, i, tail); }
public static void main(String[] args) { int[] arr = new int[]{5,5,5,5,5,5,5,5,5,5,5}; qSort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr)); } }
|