## 随机化选择标定点

import java.util.Arrays;
import java.util.Random;

public class Algorithm {

public static void main(String[] args) {

Integer[] testScale = {10000, 100000};

for (Integer n : testScale) {

Integer[] randomArr = ArrayGenerator.generatorRandomArray(n, n);

Integer[] sortedArr = ArrayGenerator.generatorSortedArray(n, n);

Integer[] arr1 = Arrays.copyOf(randomArr, randomArr.length);
Integer[] arr3 = Arrays.copyOf(randomArr, randomArr.length);

Integer[] arr2 = Arrays.copyOf(sortedArr, sortedArr.length);
Integer[] arr4 = Arrays.copyOf(sortedArr, sortedArr.length);

System.out.println("测试随机数组排序性能");
System.out.println();

Verify.testTime("MergeSort", arr1);
Verify.testTime("QuickSortOptimized", arr3);

System.out.println();

System.out.println("测试有序数组排序性能");
System.out.println();

Verify.testTime("MergeSort", arr2);
Verify.testTime("QuickSortOptimized", arr4);

System.out.println();
}
}
}

class QuickSort {

private QuickSort(){}

public static<E extends Comparable<E>> void sortOptimized(E[] arr){

/**
* 将重复使用的temp和random变量作为参数传递，进行内存优化
*/
Random random = new Random();
E temp = null;
sortOptimized(arr, 0, arr.length - 1, random, temp);
}

public static<E extends Comparable<E>> void sortOptimized(E[] arr, int left, int right, Random random, E temp){

if (left >= right){
return;
}

int p = partition(arr, left, right, random, temp);

sortOptimized(arr, left, p - 1, random, temp);
sortOptimized(arr, p + 1, right, random, temp);
}

public static <E extends Comparable<E>> int partition(E[] arr, int left, int right, Random random, E temp){

/**
* 生成[left, right]之间的随机索引
* random.nextInt()方法只能生成[0, bound)的整数，因此还要加上偏移left
*/
int p = random.nextInt(right - left + 1) + left;

temp = arr[p];
arr[p] = arr[left];
arr[left] = temp;

int j = left;

for (int i = left + 1; i <= right; i++) {

if (arr[i].compareTo(arr[left]) < 0){

j++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

temp = arr[j];
arr[j] = arr[left];
arr[left] = temp;

return j;
}
}

class MergeSort {

private MergeSort(){}

public static<E extends Comparable<E>> void sort(E[] arr){

E[] temp = Arrays.copyOf(arr, arr.length);

sort(arr, 0, arr.length - 1, temp);
}

private static<E extends Comparable<E>> void sort(E[] arr, int left, int right, E[] temp){

if (left >= right){
return;
}

int mid = left + (right - left) / 2;

sort(arr, left, mid, temp);
sort(arr, mid + 1, right, temp);

if (arr[mid].compareTo(arr[mid + 1]) > 0) {
merge(arr, left, mid, right, temp);
}
}

public static<E extends Comparable<E>> void merge(E[] arr, int left, int mid, int right, E[] temp) {

int i = left;
int j = mid + 1;

System.arraycopy(arr, left, temp, left, right - left + 1);

for (int n = left; n < right + 1; n++) {

if (i == mid + 1){
arr[n] = temp[j];
j++;
}
else if (j == right + 1) {
arr[n] = temp[i];
i++;
}
else if (temp[i].compareTo(temp[j]) <= 0) {
arr[n] = temp[i];
i++;
}
else{
arr[n] = temp[j];
j++;
}
}
}
}

class ArrayGenerator {

private ArrayGenerator (){}

public static Integer[] generatorRandomArray (Integer n, Integer maxBound){

Integer[] arr = new Integer[n];

Random random = new Random();

for (int i = 0; i < n; i++) {

arr[i] = random.nextInt(maxBound);
}

return arr;
}

public static Integer[] generatorSortedArray (Integer n, Integer maxBound){

Integer[] arr = new Integer[n];

for (int i = 0; i < n; i++) {

arr[i] = i;
}

return arr;
}
}

class Verify {

private Verify (){}

public static<E extends Comparable<E>> boolean isSorted(E[] arr){

for (int i = 0; i < arr.length - 1; i++) {
if (arr[i].compareTo(arr[i + 1]) > 0) {
return false;
}
}

return true;
}

public static<E extends Comparable<E>> void testTime(String AlgorithmName, E[] arr) {

long startTime = System.nanoTime();

if (AlgorithmName.equals("MergeSort")) {
MergeSort.sort(arr);
}

if (AlgorithmName.equals("QuickSortOptimized")) {
QuickSort.sortOptimized(arr);
}

long endTime = System.nanoTime();

if (!Verify.isSorted(arr)){
throw new RuntimeException(AlgorithmName + "算法排序失败！");
}

System.out.println(String.format("%s算法，测试用例为%d，执行时间：%f秒", AlgorithmName, arr.length, (endTime - startTime) / 1000000000.0));
}
}


import java.util.Arrays;
import java.util.Random;

public class Algorithm {

public static void main(String[] args) {

Integer[] testScale = {10000, 100000};

for (Integer n : testScale) {

Integer[] randomArr = ArrayGenerator.generatorRandomArray(n, n);

Integer[] sortedArr = ArrayGenerator.generatorSortedArray(n, n);

Integer[] specialArr = ArrayGenerator.generateSpecialArray(n);

Integer[] arr1 = Arrays.copyOf(randomArr, randomArr.length);
Integer[] arr3 = Arrays.copyOf(randomArr, randomArr.length);

Integer[] arr2 = Arrays.copyOf(sortedArr, sortedArr.length);
Integer[] arr4 = Arrays.copyOf(sortedArr, sortedArr.length);

Integer[] arr5 = Arrays.copyOf(specialArr, specialArr.length);
Integer[] arr6 = Arrays.copyOf(specialArr, specialArr.length);

System.out.println("测试随机数组排序性能");
System.out.println();

Verify.testTime("MergeSort", arr1);
Verify.testTime("QuickSortOptimized", arr3);

System.out.println();

System.out.println("测试有序数组排序性能");
System.out.println();

Verify.testTime("MergeSort", arr2);
Verify.testTime("QuickSortOptimized", arr4);

System.out.println();

System.out.println("测试特殊数组排序性能");
System.out.println();

Verify.testTime("MergeSort", arr5);
Verify.testTime("QuickSortOptimized", arr6);
}
}
}

class QuickSort {

private QuickSort(){}

public static<E extends Comparable<E>> void sortOptimized(E[] arr){

E temp = null;
sortOptimized(arr, 0, arr.length - 1, temp);
}

public static<E extends Comparable<E>> void sortOptimized(E[] arr, int left, int right, E temp){

if (left >= right){
return;
}

int p = partition(arr, left, right, temp);

sortOptimized(arr, left, p - 1, temp);
sortOptimized(arr, p + 1, right, temp);
}

public static <E extends Comparable<E>> int partition(E[] arr, int left, int right, E temp){

int mid = left + (right - left) / 2;

temp = arr[mid];
arr[mid] = arr[left];
arr[left] = temp;

int j = left;

for (int i = left + 1; i <= right; i++) {

if (arr[i].compareTo(arr[left]) < 0){

j++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

temp = arr[j];
arr[j] = arr[left];
arr[left] = temp;

return j;
}
}

class MergeSort {

private MergeSort(){}

public static<E extends Comparable<E>> void sort(E[] arr){

E[] temp = Arrays.copyOf(arr, arr.length);

sort(arr, 0, arr.length - 1, temp);
}

private static<E extends Comparable<E>> void sort(E[] arr, int left, int right, E[] temp){

if (left >= right){
return;
}

int mid = left + (right - left) / 2;

sort(arr, left, mid, temp);
sort(arr, mid + 1, right, temp);

if (arr[mid].compareTo(arr[mid + 1]) > 0) {
merge(arr, left, mid, right, temp);
}
}

public static<E extends Comparable<E>> void merge(E[] arr, int left, int mid, int right, E[] temp) {

int i = left;
int j = mid + 1;

System.arraycopy(arr, left, temp, left, right - left + 1);

for (int n = left; n < right + 1; n++) {

if (i == mid + 1){
arr[n] = temp[j];
j++;
}
else if (j == right + 1) {
arr[n] = temp[i];
i++;
}
else if (temp[i].compareTo(temp[j]) <= 0) {
arr[n] = temp[i];
i++;
}
else{
arr[n] = temp[j];
j++;
}
}
}
}

class ArrayGenerator {

private ArrayGenerator() {
}

public static Integer[] generatorRandomArray(Integer n, Integer maxBound) {

Integer[] arr = new Integer[n];

Random random = new Random();

for (int i = 0; i < n; i++) {

arr[i] = random.nextInt(maxBound);
}

return arr;
}

public static Integer[] generatorSortedArray(Integer n, Integer maxBound) {

Integer[] arr = new Integer[n];

for (int i = 0; i < n; i++) {

arr[i] = i;
}

return arr;
}

public static Integer[] generateSpecialArray(int n) {

Integer[] arr = new Integer[n];
generateSpecialArray(arr, 0, n - 1, 0);

return arr;
}

public static void generateSpecialArray(Integer[] arr, int left, int right, int value) {

if (left >= right) {
return;
}

int mid = left + (right - left) / 2;

/**
* 总是将最小值赋值给mid，这样在每个子区间，mid都是最小的元素
*/
arr[mid] = value;

generateSpecialArray(arr, left, mid, value + 1);
generateSpecialArray(arr, mid + 1, right, value + 1);

/**
* 因为mid是左区间最后一个元素，因此左区间的元素都会被赋值，可是右区间最后一个元素可能不会被递归到
* 为了防止最后一个元素没有被赋值，所以要额外判断一下
*/
if (arr[right] == null){
arr[right] = value + 2;
}
}
}

class Verify {

private Verify (){}

public static<E extends Comparable<E>> boolean isSorted(E[] arr){

for (int i = 0; i < arr.length - 1; i++) {
if (arr[i].compareTo(arr[i + 1]) > 0) {
return false;
}
}

return true;
}

public static<E extends Comparable<E>> void testTime(String AlgorithmName, E[] arr) {

long startTime = System.nanoTime();

if (AlgorithmName.equals("MergeSort")) {
MergeSort.sort(arr);
}

if (AlgorithmName.equals("QuickSortOptimized")) {
QuickSort.sortOptimized(arr);
}

long endTime = System.nanoTime();

if (!Verify.isSorted(arr)){
throw new RuntimeException(AlgorithmName + "算法排序失败！");
}

System.out.println(String.format("%s算法，测试用例为%d，执行时间：%f秒", AlgorithmName, arr.length, (endTime - startTime) / 1000000000.0));
}
}