- public static void quickSort(int[] a) {
- quickSort(a, 0, a.length - 1);
- }
- private static void quickSort(int[] a, int start, int end) {
- int left = start;
- int right = end - 1;
- int pivot = a[end];
- while (left < right) {
- if (a[left] <= pivot) {
- left++;
- continue;
- }
- if (a[right] > pivot) {
- right--;
- continue;
- }
- swap(a, left++, right);
- }
- // 左向右方向没有找到,就已经与右向左重合了。
- if (a[left] < pivot) {
- left++;
- }
- swap(a, left, end);
- if(left - 1 > start) {
- quickSort(a, start, left - 1);
- }
- if(left + 1 < end) {
- quickSort(a, left + 1, end);
- }
- }
复制代码 |