package thread;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
public class Exercise30_14 {
public static void main(String[] args) {
final int SIZE = 9_000_000;
Integer[] integers = new Integer[SIZE]; //整型数组
for (int i = 0; i < integers.length; i++) //初始化赋值
integers[i] = new SecureRandom().nextInt(SIZE);
//测试快速排序
long startTime = System.currentTimeMillis();
quickSort(integers);
long endTime = System.currentTimeMillis();
System.out.println("The sequential time is " + (endTime - startTime) + " milliseconds");
//输出前100个值
for (int i = 0; i < 100; i++)
System.out.printf("%6d", integers[i]);
System.out.println();
// String[] strings = {"China", "Paris", "France", "America", "Canada", "India", "Babylon"};
// System.out.println(Arrays.toString(strings));
// quickSort(strings);
// System.out.println(Arrays.toString(strings));
//-------------------------------------------------------
int[] list = new int[SIZE]; //整型数组
for (int i = 0; i < list.length; i++) //初始化赋值
list[i] = new SecureRandom().nextInt(SIZE);
//测试并行快速排序
startTime = System.currentTimeMillis();
parallelQuickSort(list);
endTime = System.currentTimeMillis();
System.out.println("\n\nThe number of processors is " + Runtime.getRuntime().availableProcessors());
System.out.println("The time is " + (endTime - startTime) + " milliseconds");
//输出前100个值
for (int i = 0; i < 100; i++)
System.out.printf("%6d", list[i]);
}
/** 泛型快速排序 */
public static <E extends Comparable<E>> void quickSort(E[] list) {
quickSort(list, 0, list.length - 1);
}
/** 泛型快速排序辅助方法 */
public static <E extends Comparable<E>> void quickSort(E[] list, int low, int high) {
if (low >= high) return; //处理本方法末尾的递归调用产生的下标问题
int i = low, j = high; //获取低位和高位
E temp = list[i]; //基数(低位和高位的数将与之比较)
while (i != j) {
while (i < j && list[j].compareTo(temp) >= 0) j--; //查找小于基数的数下标
while (i < j && list[i].compareTo(temp) <= 0) i++; //查找大于基数的数下标
//交换值
if (i < j) {
E t = list[i];
list[i] = list[j];
list[j] = t;
}
}
//将基数与 i == j 的数相交换,此时基数前子数组将小于基数,后子数组大于基数
list[low] = list[i];
list[i] = temp;
//对两部分子数组递归调用进行排序
quickSort(list, low, i - 1);
quickSort(list, i + 1, high);
}
/** 并行快速排序 */
public static void parallelQuickSort(int[] list) {
RecursiveAction mainTask = new QuickSortTask(list, 0, list.length - 1);
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(mainTask);
}
/** 内部类-快速排序任务 */
private static class QuickSortTask extends RecursiveAction {
private int[] list;
private int low, high;
public QuickSortTask(int[] list, int low, int high) {
this.list = list;
this.low = low;
this.high = high;
}
@Override
protected void compute() {
if (low >= high) return;
int i = low, j = high;
int temp = list[i];
while (i < j) {
while (i < j && list[j] >= temp) j--;
while (i < j && list[i] <= temp) i++;
if (i < j) {
int t = list[i];
list[i] = list[j];
list[j] = t;
}
}
list[low] = list[i];
list[i] = temp;
//相当于递归调用,但由多个处理器执行
invokeAll(new QuickSortTask(list, low, i - 1), new QuickSortTask(list, i + 1, high));
}
}
}