package class05;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* 递归方式的快排和非递归方式的快排
*/
public class Code03_QuickSortRecursiveAndNonRecursive {
//荷兰国旗问题
public static int[] netherLandsFlag(int[] arr, int L, int R) {
if (L > R) {
return new int[]{-1, -1};
}
if (L == R) {
return new int[]{L, L};
}
int index = L;
int less = L - 1;
int more = R;
while (index < more) {//没有等于,当index == more时,需要跳出循环。
if (arr[index] < arr[R]) {
swap(arr, index++, ++less);
} else if (arr[index] == arr[R]) {
index++;
} else {
swap(arr, index, --more);
}
}
swap(arr, more, R);
return new int[]{less + 1, more};
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//快排递归版本
public static void quickSort1(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
process(arr, 0, arr.length - 1);
}
private static void process(int[] arr, int L, int R) {
if (L >= R) {
return;
}
swap(arr, L + (int) (Math.random() * (R - L + 1)), R);
int[] equalArea = netherLandsFlag(arr, L, R);
process(arr, L, equalArea[0] - 1);
process(arr, equalArea[1] + 1, R);
}
//快排非递归版本需要的辅助类
//表示的是在[l, r]]范围上进行排序
public static class Op {
private int l;
private int r;
public Op(int left, int right) {
l = left;
r = right;
}
}
//快排非递归版本,用栈来实现
public static void quickSort2(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
swap(arr, (int) (Math.random() * arr.length), arr.length - 1);
int[] equalArea = netherLandsFlag(arr, 0, arr.length - 1);//等于arr[R]区
Stack<Op> stack = new Stack<>();
stack.push(new Op(0, equalArea[0] - 1));
stack.push(new Op(equalArea[1] + 1, arr.length - 1));
while (!stack.isEmpty()) {
Op op = stack.pop();//弹出一个任务,进行处理。在[op.l, op.r]范围上进行排序。
if (op.l < op.r) {
swap(arr, op.l + (int) (Math.random() * (op.r - op.l + 1)), op.r);
equalArea = netherLandsFlag(arr, op.l, op.r);
stack.push(new Op(op.l, equalArea[0] - 1));
stack.push(new Op(equalArea[1] + 1, op.r));
}
}
}
//快排非递归版本,使用队列实现
public static void quickSort3(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
int N = arr.length;
swap(arr, (int) (Math.random() * N), N - 1);
int[] equalArea = netherLandsFlag(arr, 0, N - 1);//等于arr[R]区
Queue<Op> queue = new LinkedList<>();
queue.offer(new Op(0, equalArea[0] - 1));
queue.offer(new Op(equalArea[1] + 1, N - 1));
while (!queue.isEmpty()) {
Op op = queue.poll();//弹出一个任务,进行处理。在[op.l, op.r]范围上进行排序。
if (op.l < op.r) {
swap(arr, op.l + (int) (Math.random() * (op.r - op.l + 1)), op.r);
equalArea = netherLandsFlag(arr, op.l, op.r);
queue.offer(new Op(op.l, equalArea[0] - 1));
queue.offer(new Op(equalArea[1] + 1, op.r));
}
}
}
// for test
public static int[] generateRandomArray(int maxSize, int maxValue) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
}
return arr;
}
// for test
public static int[] copyArray(int[] arr) {
if (arr == null) {
return null;
}
int[] res = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
// for test
public static boolean isEqual(int[] arr1, int[] arr2) {
if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
return false;
}
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
// for test
public static void printArray(int[] arr) {
if (arr == null) {
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int maxSize = 100;
int maxValue = 100;
int testTimes = 100000;
System.out.println("test start!");
boolean flag = true;
for (int i = 0; i < testTimes; i++) {
int[] arr0 = generateRandomArray(maxSize, maxValue);
int[] arr1 = copyArray(arr0);
int[] arr2 = copyArray(arr0);
int[] arr3 = copyArray(arr0);
int[] arr4 = copyArray(arr0);
Arrays.sort(arr4);
quickSort1(arr1);
quickSort2(arr2);
quickSort3(arr3);
if (!isEqual(arr1, arr4) || !isEqual(arr2, arr4) || !isEqual(arr3, arr4)) {
System.out.println("arr0 = " + Arrays.toString(arr0));//原始数组
System.out.println("arr1 = " + Arrays.toString(arr1));//quickSort1()
System.out.println("arr2 = " + Arrays.toString(arr2));//quickSort2()
System.out.println("arr3 = " + Arrays.toString(arr3));//quickSort3()
System.out.println("arr4 = " + Arrays.toString(arr4));//对照数组
flag = false;
break;
}
}
System.out.println("test end!");
System.out.println(flag ? "nice!" : "oops!");
}
}