diff --git a/src/class03/Code04_BSAwesome.java b/src/class03/Code04_BSAwesome.java index 3350646..f40b167 100644 --- a/src/class03/Code04_BSAwesome.java +++ b/src/class03/Code04_BSAwesome.java @@ -2,7 +2,6 @@ public class Code04_BSAwesome { - // arr 整体无序 // arr 相邻的数不相等! public static int oneMinIndex(int[] arr) { if (arr == null || arr.length == 0) { diff --git a/src/class04/Code04_ReverseNodesInKGroup.java b/src/class04/Code04_ReverseNodesInKGroup.java index 9a8dc12..42eaed6 100644 --- a/src/class04/Code04_ReverseNodesInKGroup.java +++ b/src/class04/Code04_ReverseNodesInKGroup.java @@ -1,57 +1,59 @@ -package class04; - -// 测试链接:https://leetcode.com/problems/reverse-nodes-in-k-group/ -public class Code04_ReverseNodesInKGroup { - - // 不要提交这个类 - public static class ListNode { - public int val; - public ListNode next; - } - - public static ListNode reverseKGroup(ListNode head, int k) { - ListNode start = head; - ListNode end = getKGroupEnd(start, k); - if (end == null) { - return head; - } - // 第一组凑齐了! - head = end; - reverse(start, end); - // 上一组的结尾节点 - ListNode lastEnd = start; - while (lastEnd.next != null) { - start = lastEnd.next; - end = getKGroupEnd(start, k); - if (end == null) { - return head; - } - reverse(start, end); - lastEnd.next = end; - lastEnd = start; - } - return head; - } - - public static ListNode getKGroupEnd(ListNode start, int k) { - while (--k != 0 && start != null) { - start = start.next; - } - return start; - } - - public static void reverse(ListNode start, ListNode end) { - end = end.next; - ListNode pre = null; - ListNode cur = start; - ListNode next = null; - while (cur != end) { - next = cur.next; - cur.next = pre; - pre = cur; - cur = next; - } - start.next = end; - } - -} + + +package class04; + +// 测试链接:https://leetcode.com/problems/reverse-nodes-in-k-group/ +public class Code04_ReverseNodesInKGroup { + + // 不要提交这个类 + public static class ListNode { + public int val; + public ListNode next; + } + + public static ListNode reverseKGroup(ListNode head, int k) { + ListNode start = head; + ListNode end = getKGroupEnd(start, k); + if (end == null) { + return head; + } + // 第一组凑齐了! + head = end; + reverse(start, end); + // 上一组的结尾节点 + ListNode lastEnd = start; + while (lastEnd.next != null) { + start = lastEnd.next; + end = getKGroupEnd(start, k); + if (end == null) { + return head; + } + reverse(start, end); + lastEnd.next = end; + lastEnd = start; + } + return head; + } + + public static ListNode getKGroupEnd(ListNode start, int k) { + while (--k != 0 && start != null) { + start = start.next; + } + return start; + } + + public static void reverse(ListNode start, ListNode end) { + end = end.next; + ListNode pre = null; + ListNode cur = start; + ListNode next = null; + while (cur != end) { + next = cur.next; + cur.next = pre; + pre = cur; + cur = next; + } + start.next = end; + } + +} \ No newline at end of file diff --git a/src/class06/Code06_BinaryTreeLevelOrderTraversalII.java b/src/class06/Code06_BinaryTreeLevelOrderTraversalII.java deleted file mode 100644 index 3269d13..0000000 --- a/src/class06/Code06_BinaryTreeLevelOrderTraversalII.java +++ /dev/null @@ -1,45 +0,0 @@ -package class06; - -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -// 测试链接:https://leetcode.com/problems/binary-tree-level-order-traversal-ii -public class Code06_BinaryTreeLevelOrderTraversalII { - - public static class TreeNode { - public int val; - public TreeNode left; - public TreeNode right; - - TreeNode(int val) { - this.val = val; - } - } - - public List> levelOrderBottom(TreeNode root) { - List> ans = new LinkedList<>(); - if (root == null) { - return ans; - } - Queue queue = new LinkedList<>(); - queue.add(root); - while (!queue.isEmpty()) { - int size = queue.size(); - List curAns = new LinkedList<>(); - for (int i = 0; i < size; i++) { - TreeNode curNode = queue.poll(); - curAns.add(curNode.val); - if (curNode.left != null) { - queue.add(curNode.left); - } - if (curNode.right != null) { - queue.add(curNode.right); - } - } - ans.add(0, curAns); - } - return ans; - } - -} diff --git a/src/class06/Code07_BalancedBinaryTree.java b/src/class06/Code07_BalancedBinaryTree.java deleted file mode 100644 index 6d53259..0000000 --- a/src/class06/Code07_BalancedBinaryTree.java +++ /dev/null @@ -1,42 +0,0 @@ -package class06; - -// 测试链接:https://leetcode.com/problems/balanced-binary-tree -public class Code07_BalancedBinaryTree { - - public static class TreeNode { - public int val; - public TreeNode left; - public TreeNode right; - - TreeNode(int val) { - this.val = val; - } - } - - public static class Info { - public boolean isBalanced; - public int height; - - public Info(boolean i, int h) { - isBalanced = i; - height = h; - } - } - - public static boolean isBalanced(TreeNode root) { - return process(root).isBalanced; - } - - public static Info process(TreeNode root) { - if (root == null) { - return new Info(true, 0); - } - Info leftInfo = process(root.left); - Info rightInfo = process(root.right); - int height = Math.max(leftInfo.height, rightInfo.height) + 1; - boolean isBalanced = leftInfo.isBalanced && rightInfo.isBalanced - && Math.abs(leftInfo.height - rightInfo.height) < 2; - return new Info(isBalanced, height); - } - -} diff --git a/src/class06/Code08_PathSum.java b/src/class06/Code08_PathSum.java deleted file mode 100644 index aa9a4dc..0000000 --- a/src/class06/Code08_PathSum.java +++ /dev/null @@ -1,32 +0,0 @@ -package class06; - -public class Code08_PathSum { - - // 测试链接:https://leetcode.com/problems/path-sum - public static class TreeNode { - public int val; - public TreeNode left; - public TreeNode right; - - TreeNode(int val) { - this.val = val; - } - } - - public static boolean hasPathSum(TreeNode root, int sum) { - if (root == null) { - return false; - } - return process(root, sum); - } - - public static boolean process(TreeNode root, int rest) { - if (root.left == null && root.right == null) { - return root.val == rest; - } - boolean ans = root.left != null ? process(root.left, rest - root.val) : false; - ans |= root.right != null ? process(root.right, rest - root.val) : false; - return ans; - } - -} diff --git a/src/class06/Code09_PathSumII.java b/src/class06/Code09_PathSumII.java deleted file mode 100644 index 2a89f0b..0000000 --- a/src/class06/Code09_PathSumII.java +++ /dev/null @@ -1,55 +0,0 @@ -package class06; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - -public class Code09_PathSumII { - - // 测试链接:https://leetcode.com/problems/path-sum-ii - public static class TreeNode { - public int val; - public TreeNode left; - public TreeNode right; - - TreeNode(int val) { - this.val = val; - } - } - - public static List> pathSum(TreeNode root, int sum) { - List> ans = new ArrayList<>(); - if (root == null) { - return ans; - } - LinkedList path = new LinkedList<>(); - process(root, sum, path, ans); - return ans; - } - - public static void process(TreeNode root, int rest, LinkedList path, List> ans) { - path.addLast(root.val); - if (root.left == null && root.right == null) { - if (root.val == rest) { - ans.add(copy(path)); - } - } else { - if (root.left != null) { - process(root.left, rest - root.val, path, ans); - } - if (root.right != null) { - process(root.right, rest - root.val, path, ans); - } - } - path.pollLast(); - } - - public static List copy(LinkedList path) { - List ans = new ArrayList<>(); - for (Integer num : path) { - ans.add(num); - } - return ans; - } - -} diff --git a/src/class07/Code03_PathSum.java b/src/class07/Code03_PathSum.java index 9bde28b..0f33665 100644 --- a/src/class07/Code03_PathSum.java +++ b/src/class07/Code03_PathSum.java @@ -13,20 +13,48 @@ public static class TreeNode { } } + public static boolean isSum = false; + public static boolean hasPathSum(TreeNode root, int sum) { if (root == null) { return false; } - return process(root, sum); + isSum = false; + process(root, 0, sum); + return isSum; } - public static boolean process(TreeNode root, int rest) { - if (root.left == null && root.right == null) { - return root.val == rest; + public static void process(TreeNode x, int preSum, int sum) { + if (x.left == null && x.right == null) { + if (x.val + preSum == sum) { + isSum = true; + } + return; + } + // x是非叶节点 + preSum += x.val; + if (x.left != null) { + process(x.left, preSum, sum); + } + if (x.right != null) { + process(x.right, preSum, sum); } - boolean ans = root.left != null ? process(root.left, rest - root.val) : false; - ans |= root.right != null ? process(root.right, rest - root.val) : false; - return ans; } +// public static boolean hasPathSum(TreeNode root, int sum) { +// if (root == null) { +// return false; +// } +// return process(root, sum); +// } +// +// public static boolean process(TreeNode root, int rest) { +// if (root.left == null && root.right == null) { +// return root.val == rest; +// } +// boolean ans = root.left != null ? process(root.left, rest - root.val) : false; +// ans |= root.right != null ? process(root.right, rest - root.val) : false; +// return ans; +// } + } diff --git a/src/class07/Code04_PathSumII.java b/src/class07/Code04_PathSumII.java index cec6e6c..50171eb 100644 --- a/src/class07/Code04_PathSumII.java +++ b/src/class07/Code04_PathSumII.java @@ -1,7 +1,6 @@ package class07; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; public class Code04_PathSumII { @@ -22,29 +21,33 @@ public static List> pathSum(TreeNode root, int sum) { if (root == null) { return ans; } - LinkedList path = new LinkedList<>(); - process(root, sum, path, ans); + ArrayList path = new ArrayList<>(); + process(root, path, 0, sum, ans); return ans; } - public static void process(TreeNode root, int rest, LinkedList path, List> ans) { - path.addLast(root.val); - if (root.left == null && root.right == null) { - if (root.val == rest) { + public static void process(TreeNode x, List path, int preSum, int sum, List> ans) { + if (x.left == null && x.right == null) { + if (preSum + x.val == sum) { + path.add(x.val); ans.add(copy(path)); + path.remove(path.size() - 1); } - } else { - if (root.left != null) { - process(root.left, rest - root.val, path, ans); - } - if (root.right != null) { - process(root.right, rest - root.val, path, ans); - } + return; + } + // x 非叶节点 + path.add(x.val); + preSum += x.val; + if (x.left != null) { + process(x.left, path, preSum, sum, ans); + } + if (x.right != null) { + process(x.right, path, preSum, sum, ans); } - path.pollLast(); + path.remove(path.size() - 1); } - public static List copy(LinkedList path) { + public static List copy(List path) { List ans = new ArrayList<>(); for (Integer num : path) { ans.add(num); diff --git a/src/class07/Code05_GetMax.java b/src/class07/Code05_GetMax.java deleted file mode 100644 index d3f3e0b..0000000 --- a/src/class07/Code05_GetMax.java +++ /dev/null @@ -1,44 +0,0 @@ -package class07; - -public class Code05_GetMax { - - public static int flip(int n) { - return n ^ 1; - } - - public static int sign(int n) { - - return flip((n >> 31) & 1); - } - - public static int getMax1(int a, int b) { - int c = a - b; - int scA = sign(c); - int scB = flip(scA); - return a * scA + b * scB; - } - - public static int getMax2(int a, int b) { - int c = a - b; - int sa = sign(a); - int sb = sign(b); - int sc = sign(c); - int difSab = sa ^ sb; - int sameSab = flip(difSab); - int returnA = difSab * sa + sameSab * sc; - int returnB = flip(returnA); - return a * returnA + b * returnB; - } - - public static void main(String[] args) { - int a = -16; - int b = -19; - System.out.println(getMax1(a, b)); - System.out.println(getMax2(a, b)); - a = 2147483647; - b = -2147480000; - System.out.println(getMax1(a, b)); // wrong answer because of overflow - System.out.println(getMax2(a, b)); - } - -} diff --git a/src/class07/Code05_IsBinarySearchTree.java b/src/class07/Code05_IsBinarySearchTree.java new file mode 100644 index 0000000..fa761f5 --- /dev/null +++ b/src/class07/Code05_IsBinarySearchTree.java @@ -0,0 +1,85 @@ +package class07; + +public class Code05_IsBinarySearchTree { + + public static class TreeNode { + public int val; + public TreeNode left; + public TreeNode right; + + TreeNode(int val) { + this.val = val; + } + } + + public static class Info { + public boolean isBST; + public int max; + public int min; + + public Info(boolean is, int ma, int mi) { + isBST = is; + max = ma; + min = mi; + } + } + +// public static Info process(TreeNode x) { +// if (x == null) { +// return null; +// } +// Info leftInfo = process(x.left); +// Info rightInfo = process(x.right); +// int max = x.val; +// int min = x.val; +// if (leftInfo != null) { +// max = Math.max(leftInfo.max, max); +// min = Math.min(leftInfo.min, min); +// } +// if (rightInfo != null) { +// max = Math.max(rightInfo.max, max); +// min = Math.min(rightInfo.min, min); +// } +// boolean isBST = true; +// if (leftInfo != null && !leftInfo.isBST) { +// isBST = false; +// } +// if (rightInfo != null && !rightInfo.isBST) { +// isBST = false; +// } +// boolean leftMaxLessX = leftInfo == null ? true : (leftInfo.max < x.val); +// boolean rightMinMoreX = rightInfo == null ? true : (rightInfo.min > x.val); +// if (!(leftMaxLessX && rightMinMoreX)) { +// isBST = false; +// } +// return new Info(isBST, max, min); +// } + + public static Info process(TreeNode x) { + if (x == null) { + return null; + } + Info leftInfo = process(x.left); + Info rightInfo = process(x.right); + int max = x.val; + int min = x.val; + if (leftInfo != null) { + max = Math.max(leftInfo.max, max); + min = Math.min(leftInfo.min, min); + } + if (rightInfo != null) { + max = Math.max(rightInfo.max, max); + min = Math.min(rightInfo.min, min); + } + boolean isBST = false; + boolean leftIsBst = leftInfo == null ? true : leftInfo.isBST; + boolean rightIsBst = rightInfo == null ? true : rightInfo.isBST; + boolean leftMaxLessX = leftInfo == null ? true : (leftInfo.max < x.val); + boolean rightMinMoreX = rightInfo == null ? true : (rightInfo.min > x.val); + if (leftIsBst && rightIsBst && leftMaxLessX && rightMinMoreX) { + isBST = true; + } + return new Info(isBST, max, min); + } + +} diff --git a/src/class06/Code10_GetMax.java b/src/class08/Code01_GetMax.java similarity index 94% rename from src/class06/Code10_GetMax.java rename to src/class08/Code01_GetMax.java index ba025aa..19bf7df 100644 --- a/src/class06/Code10_GetMax.java +++ b/src/class08/Code01_GetMax.java @@ -1,13 +1,12 @@ -package class06; +package class08; -public class Code10_GetMax { +public class Code01_GetMax { public static int flip(int n) { return n ^ 1; } public static int sign(int n) { - return flip((n >> 31) & 1); } diff --git a/src/class08/Code02_MergeSort.java b/src/class08/Code02_MergeSort.java new file mode 100644 index 0000000..b46401b --- /dev/null +++ b/src/class08/Code02_MergeSort.java @@ -0,0 +1,183 @@ +package class08; + +// 可以去体系学习班学习 +public class Code02_MergeSort { + + // 递归方法实现 + public static void mergeSort1(int[] arr) { + if (arr == null || arr.length < 2) { + return; + } + process(arr, 0, arr.length - 1); + } + + // arr[L...R]范围上,请让这个范围上的数,有序! + public static void process(int[] arr, int L, int R) { + if (L == R) { + return; + } + // int mid = (L + R) / 2 + int mid = L + ((R - L) >> 1); + process(arr, L, mid); + process(arr, mid + 1, R); + merge(arr, L, mid, R); + } + + public static void merge(int[] arr, int L, int M, int R) { + int[] help = new int[R - L + 1]; + int i = 0; + int p1 = L; + int p2 = M + 1; + while (p1 <= M && p2 <= R) { + help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++]; + } + // 要么p1越界,要么p2越界 + // 不可能出现:共同越界 + while (p1 <= M) { + help[i++] = arr[p1++]; + } + while (p2 <= R) { + help[i++] = arr[p2++]; + } + for (i = 0; i < help.length; i++) { + arr[L + i] = help[i]; + } + } + + public static void mergeSort2(int[] arr) { + if (arr == null || arr.length < 2) { + return; + } + int step = 1; + int N = arr.length; + while (step < N) { + int L = 0; + while (L < N) { + int M = 0; + if (N - L >= step) { + M = L + step - 1; + } else { + M = N - 1; + } + if (M == N - 1) { + break; + } + int R = 0; + if (N - 1 - M >= step) { + R = M + step; + } else { + R = N - 1; + } + merge(arr, L, M, R); + if (R == N - 1) { + break; + } else { + L = R + 1; + } + } + if (step > N / 2) { + break; + } + step *= 2; + } + + } + + // 非递归方法实现 +// public static void mergeSort2(int[] arr) { +// if (arr == null || arr.length < 2) { +// return; +// } +// int N = arr.length; +// int mergeSize = 1; +// while (mergeSize < N) { +// int L = 0; +// while (L < N) { +// if (mergeSize >= N - L) { +// break; +// } +// int M = L + mergeSize - 1; +// int R = M + Math.min(mergeSize, N - M - 1); +// merge(arr, L, M, R); +// L = R + 1; +// } +// if (mergeSize > N / 2) { +// break; +// } +// mergeSize <<= 1; +// } +// } + + // 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(); + } + + // for test + public static void main(String[] args) { + int testTime = 500000; + int maxSize = 100; + int maxValue = 100; + System.out.println("测试开始"); + for (int i = 0; i < testTime; i++) { + int[] arr1 = generateRandomArray(maxSize, maxValue); + int[] arr2 = copyArray(arr1); + mergeSort1(arr1); + mergeSort2(arr2); + if (!isEqual(arr1, arr2)) { + System.out.println("出错了!"); + printArray(arr1); + printArray(arr2); + break; + } + } + System.out.println("测试结束"); + } + +} diff --git a/src/class08/Code03_PartitionAndQuickSort.java b/src/class08/Code03_PartitionAndQuickSort.java new file mode 100644 index 0000000..52635a9 --- /dev/null +++ b/src/class08/Code03_PartitionAndQuickSort.java @@ -0,0 +1,231 @@ +package class08; + +import java.util.Stack; + +// 可以去体系学习班学习 +public class Code03_PartitionAndQuickSort { + + public static void splitNum1(int[] arr) { + int lessEqualR = -1; + int index = 0; + int N = arr.length; + while (index < N) { + if (arr[index] <= arr[N - 1]) { + swap(arr, ++lessEqualR, index++); + } else { + index++; + } + } + } + + public static void splitNum2(int[] arr) { + int N = arr.length; + int lessR = -1; + int moreL = N - 1; + int index = 0; + // arr[N-1] + while (index < moreL) { + if (arr[index] < arr[N - 1]) { + swap(arr, ++lessR, index++); + } else if (arr[index] > arr[N - 1]) { + swap(arr, --moreL, index); + } else { + index++; + } + } + swap(arr, moreL, N - 1); + } + + public static void swap(int[] arr, int i, int j) { + int tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } + + // arr[L...R]范围上,拿arr[R]做划分值, + // L....R < = > + public static int[] partition(int[] arr, int L, int R) { + int lessR = L - 1; + int moreL = R; + int index = L; + while (index < moreL) { + if (arr[index] < arr[R]) { + swap(arr, ++lessR, index++); + } else if (arr[index] > arr[R]) { + swap(arr, --moreL, index); + } else { + index++; + } + } + swap(arr, moreL, R); + return new int[] { lessR + 1, moreL }; + } + + public static void quickSort1(int[] arr) { + if (arr == null || arr.length < 2) { + return; + } + process(arr, 0, arr.length - 1); + } + + public static void process(int[] arr, int L, int R) { + if (L >= R) { + return; + } + int[] equalE = partition(arr, L, R); + process(arr, L, equalE[0] - 1); + process(arr, equalE[1] + 1, R); + } + + public static class Job { + public int L; + public int R; + + public Job(int left, int right) { + L = left; + R = right; + } + } + + public static void quickSort2(int[] arr) { + if (arr == null || arr.length < 2) { + return; + } + Stack stack = new Stack<>(); + stack.push(new Job(0, arr.length - 1)); + while (!stack.isEmpty()) { + Job cur = stack.pop(); + int[] equals = partition(arr, cur.L, cur.R); + if (equals[0] > cur.L) { // 有< 区域 + stack.push(new Job(cur.L, equals[0] - 1)); + } + if (equals[1] < cur.R) { // 有 > 区域 + stack.push(new Job(equals[1] + 1, cur.R)); + } + } + } + + 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, R }; + } + int less = L - 1; + int more = R; + int index = L; + while (index < more) { + if (arr[index] == arr[R]) { + index++; + } else if (arr[index] < arr[R]) { + swap(arr, index++, ++less); + } else { + swap(arr, index, --more); + } + } + swap(arr, more, R); // <[R] =[R] >[R] + return new int[] { less + 1, more }; + } + + public static void quickSort3(int[] arr) { + if (arr == null || arr.length < 2) { + return; + } + process3(arr, 0, arr.length - 1); + } + + public static void process3(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); + process3(arr, L, equalArea[0] - 1); + process3(arr, equalArea[1] + 1, 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(); + } + + // for test + public static void main(String[] args) { +// int[] arr = { 7, 1, 3, 5, 4, 5, 1, 4, 2, 4, 2, 4 }; +// +// splitNum2(arr); +// for (int i = 0; i < arr.length; i++) { +// System.out.print(arr[i] + " "); +// } + + int testTime = 500000; + int maxSize = 100; + int maxValue = 100; + boolean succeed = true; + System.out.println("test begin"); + for (int i = 0; i < testTime; i++) { + int[] arr1 = generateRandomArray(maxSize, maxValue); + int[] arr2 = copyArray(arr1); + int[] arr3 = copyArray(arr1); + quickSort1(arr1); + quickSort2(arr2); + quickSort3(arr3); + if (!isEqual(arr1, arr2) || !isEqual(arr1, arr3)) { + System.out.println("Oops!"); + succeed = false; + break; + } + } + System.out.println("test end"); + System.out.println(succeed ? "Nice!" : "Oops!"); + + } + +}