2021-12-28 12:33:27 +00:00
|
|
|
|
/*
|
|
|
|
|
* The MIT License (MIT)
|
|
|
|
|
*
|
2022-03-13 08:05:01 +00:00
|
|
|
|
* Copyright © 2021 xrv <xrv@live.com>
|
2021-12-28 12:33:27 +00:00
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package io.github.ehlxr.algorithm.sort;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author ehlxr
|
|
|
|
|
* @since 2021-12-28 19:57.
|
|
|
|
|
*/
|
|
|
|
|
public class QuickSort {
|
|
|
|
|
public static void main(String[] args) {
|
2021-12-28 16:21:10 +00:00
|
|
|
|
int[] a = new int[]{3, 9, 5, 7, 1, 2};
|
2022-03-18 14:32:56 +00:00
|
|
|
|
sort2(a, 0, a.length - 1);
|
2021-12-28 12:33:27 +00:00
|
|
|
|
|
2021-12-28 16:21:10 +00:00
|
|
|
|
System.out.println(Arrays.toString(a));
|
2021-12-28 12:33:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 16:21:10 +00:00
|
|
|
|
public static void sort(int[] a, int p, int r) {
|
2021-12-28 12:33:27 +00:00
|
|
|
|
if (p >= r) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 16:21:10 +00:00
|
|
|
|
int q = partition(a, p, r);
|
2021-12-28 12:33:27 +00:00
|
|
|
|
|
2021-12-28 16:21:10 +00:00
|
|
|
|
sort(a, p, q - 1);
|
|
|
|
|
sort(a, q + 1, r);
|
2021-12-28 12:33:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 16:21:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* 获取分区点
|
|
|
|
|
* <p>
|
|
|
|
|
* 图示:https://cdn.jsdelivr.net/gh/0vo/oss/images/quick_sort.jpg
|
|
|
|
|
* <p>
|
|
|
|
|
* 通过游标 i 把 a[p...r] 分成两部分。
|
|
|
|
|
* a[p...i-1] 的元素都是小于 pivot 的,我们暂且叫它 “已处理区间”,a[i...r] 是 “未处理区间”。
|
|
|
|
|
* 我们每次都从未处理的区间 a[i...r] 中取一个元素 a[j],与 pivot 对比,
|
|
|
|
|
* 如果小于 pivot,则将其加入到已处理区间的尾部,也就是 a[i] 的位置
|
|
|
|
|
* <p>
|
|
|
|
|
* 只需要将 a[i] 与 a[j] 交换,就可以在 O(1) 时间复杂度内将 a[j] 放到下标为 i 的位置。
|
|
|
|
|
*/
|
|
|
|
|
public static int partition(int[] a, int p, int r) {
|
2021-12-28 12:33:27 +00:00
|
|
|
|
int i = p;
|
2021-12-28 16:21:10 +00:00
|
|
|
|
int pivot = a[r];
|
2021-12-28 12:33:27 +00:00
|
|
|
|
for (int j = p; j < r; j++) {
|
2021-12-28 16:21:10 +00:00
|
|
|
|
if (a[j] < pivot) {
|
|
|
|
|
swap(a, i++, j);
|
2021-12-28 12:33:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 16:21:10 +00:00
|
|
|
|
swap(a, i, r);
|
|
|
|
|
System.out.println("i=" + i + " " + Arrays.toString(a));
|
2021-12-28 12:33:27 +00:00
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 16:21:10 +00:00
|
|
|
|
public static void swap(int[] a, int i, int j) {
|
|
|
|
|
if (i == j) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int temp = a[i];
|
|
|
|
|
a[i] = a[j];
|
|
|
|
|
a[j] = temp;
|
2021-12-28 12:33:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 14:32:56 +00:00
|
|
|
|
public static void sort2(int[] a, int l, int r) {
|
|
|
|
|
if (a == null || l >= r) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int i = l, j = r;
|
|
|
|
|
int p = l; // 选择最左边的元素为 pivot
|
|
|
|
|
while (l < r) {
|
|
|
|
|
// 如果选择 p = l 必须先从右边找到小于 a[p] 的第一个元素
|
|
|
|
|
while (l < r && a[r] >= a[p]) {
|
|
|
|
|
r--;
|
|
|
|
|
}
|
|
|
|
|
swap(a, r, p);
|
|
|
|
|
p = r;
|
2021-12-28 12:33:27 +00:00
|
|
|
|
|
2022-03-18 14:32:56 +00:00
|
|
|
|
// 从左边找到大于 a[p] 的第一个元素
|
|
|
|
|
while (l < r && a[l] <= a[p]) {
|
|
|
|
|
l++;
|
|
|
|
|
}
|
|
|
|
|
swap(a, l, p);
|
|
|
|
|
p = l;
|
|
|
|
|
System.out.println(Arrays.toString(a));
|
|
|
|
|
|
2022-03-18 14:38:16 +00:00
|
|
|
|
}
|
2022-03-18 14:32:56 +00:00
|
|
|
|
|
|
|
|
|
sort2(a, i, p - 1);
|
|
|
|
|
sort2(a, p + 1, j);
|
|
|
|
|
}
|
2021-12-28 12:33:27 +00:00
|
|
|
|
}
|