diff --git a/src/class03_04/Code01_MergeSort.java b/src/class03_04/Code01_MergeSort.java index 154545f..a9d0448 100644 --- a/src/class03_04/Code01_MergeSort.java +++ b/src/class03_04/Code01_MergeSort.java @@ -11,7 +11,7 @@ public class Code01_MergeSort { } // 请把arr[L..R]排有序 - // l...r N + // l...r N // T(N) = 2 * T(N / 2) + O(N) // O(N * logN) public static void process(int[] arr, int L, int R) { @@ -56,12 +56,11 @@ public class Code01_MergeSort { // 当前左组的,第一个位置 int L = 0; while (L < N) { - int M = L + mergeSize - 1; - if (M >= N) { + if (mergeSize >= N - L) { break; } - int R = Math.min(M + mergeSize, N - 1); - // L .... M M + 1..... R + int M = L + mergeSize - 1; + int R = M + Math.min(mergeSize, N - M - 1); merge(arr, L, M, R); L = R + 1; } @@ -129,20 +128,20 @@ public class Code01_MergeSort { int testTime = 500000; int maxSize = 100; int maxValue = 100; - boolean succeed = true; + 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)) { - succeed = false; + System.out.println("出错了!"); printArray(arr1); printArray(arr2); break; } } - System.out.println(succeed ? "Nice!" : "Oops!"); + System.out.println("测试结束"); } } diff --git a/src/class03_05/Code01_CountOfRangeSum.java b/src/class03_05/Code01_CountOfRangeSum.java index d9f42bb..5e703bd 100644 --- a/src/class03_05/Code01_CountOfRangeSum.java +++ b/src/class03_05/Code01_CountOfRangeSum.java @@ -29,6 +29,7 @@ public class Code01_CountOfRangeSum { int ans = 0; int windowL = L; int windowR = L; + // [windowL, windowR) for (int i = M + 1; i <= R; i++) { long min = arr[i] - upper; long max = arr[i] - lower; @@ -38,7 +39,7 @@ public class Code01_CountOfRangeSum { while (windowL <= M && arr[windowL] < min) { windowL++; } - ans += Math.max(0, windowR - windowL); + ans += windowR - windowL; } long[] help = new long[R - L + 1]; int i = 0; diff --git a/src/class03_05/Code02_PartitionAndQuickSort.java b/src/class03_05/Code02_PartitionAndQuickSort.java index 7f623a2..2e5a1d0 100644 --- a/src/class03_05/Code02_PartitionAndQuickSort.java +++ b/src/class03_05/Code02_PartitionAndQuickSort.java @@ -8,6 +8,9 @@ public class Code02_PartitionAndQuickSort { arr[j] = tmp; } + // arr[L..R]上,以arr[R]位置的数做划分值 + // <= X > X + // <= X X public static int partition(int[] arr, int L, int R) { if (L > R) { return -1; @@ -28,27 +31,30 @@ public class Code02_PartitionAndQuickSort { } // arr[L...R] 玩荷兰国旗问题的划分,以arr[R]做划分值 - // arr[R] + // arr[R] public static int[] netherlandsFlag(int[] arr, int L, int R) { - if (L > R) { + if (L > R) { // L...R L>R return new int[] { -1, -1 }; } if (L == R) { return new int[] { L, R }; } int less = L - 1; // < 区 右边界 - int more = R; // > 区 左边界 + int more = R; // > 区 左边界 int index = L; - while (index < more) { + while (index < more) { // 当前位置,不能和 >区的左边界撞上 if (arr[index] == arr[R]) { index++; } else if (arr[index] < arr[R]) { +// swap(arr, less + 1, index); +// less++; +// index++; swap(arr, index++, ++less); } else { // > swap(arr, index, --more); } } - swap(arr, more, R); + swap(arr, more, R); // <[R] =[R] >[R] return new int[] { less + 1, more }; } @@ -63,12 +69,17 @@ public class Code02_PartitionAndQuickSort { if (L >= R) { return; } - // L..R partition arr[R] [ <=arr[R] arr[R] >arr[R] ] + // L..R partition arr[R] [ <=arr[R] arr[R] >arr[R] ] int M = partition(arr, L, R); process1(arr, L, M - 1); process1(arr, M + 1, R); } + + + + + public static void quickSort2(int[] arr) { if (arr == null || arr.length < 2) { return; @@ -76,15 +87,23 @@ public class Code02_PartitionAndQuickSort { process2(arr, 0, arr.length - 1); } + // arr[L...R] 排有序,快排2.0方式 public static void process2(int[] arr, int L, int R) { if (L >= R) { return; } + // [ equalArea[0] , equalArea[0]] int[] equalArea = netherlandsFlag(arr, L, R); process2(arr, L, equalArea[0] - 1); process2(arr, equalArea[1] + 1, R); } + + + + + + public static void quickSort3(int[] arr) { if (arr == null || arr.length < 2) { return; diff --git a/src/class03_05/Code03_QuickSortRecursiveAndUnrecursive.java b/src/class03_05/Code03_QuickSortRecursiveAndUnrecursive.java index f567345..da05cf5 100644 --- a/src/class03_05/Code03_QuickSortRecursiveAndUnrecursive.java +++ b/src/class03_05/Code03_QuickSortRecursiveAndUnrecursive.java @@ -53,6 +53,7 @@ public class Code03_QuickSortRecursiveAndUnrecursive { } // 快排非递归版本需要的辅助类 + // 要处理的是什么范围上的排序 public static class Op { public int l; public int r; @@ -63,7 +64,7 @@ public class Code03_QuickSortRecursiveAndUnrecursive { } } - // 快排非递归版本 + // 快排3.0 非递归版本 public static void quickSort2(int[] arr) { if (arr == null || arr.length < 2) { return; @@ -77,7 +78,7 @@ public class Code03_QuickSortRecursiveAndUnrecursive { stack.push(new Op(0, el - 1)); stack.push(new Op(er + 1, N - 1)); while (!stack.isEmpty()) { - Op op = stack.pop(); + 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); diff --git a/src/class04_06/Code01_Comparator.java b/src/class04_06/Code01_Comparator.java index ef183a0..fc3c95c 100644 --- a/src/class04_06/Code01_Comparator.java +++ b/src/class04_06/Code01_Comparator.java @@ -1,8 +1,10 @@ package class04_06; +import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; +import java.util.TreeMap; import java.util.TreeSet; public class Code01_Comparator { @@ -19,70 +21,52 @@ public class Code01_Comparator { } } - public static class IdAscendingComparator - - implements Comparator { - - // 返回负数的时候,第一个参数排在前面 - // 返回正数的时候,第二个参数排在前面 - // 返回0的时候,谁在前面无所谓 - @Override - public int compare(Student o1, Student o2) { - return o1.id - o2.id; - } - - } - - public static class IdDescendingComparator implements Comparator { + // 任何比较器: + // compare方法里,遵循一个统一的规范: + // 返回负数的时候,认为第一个参数应该排在前面 + // 返回正数的时候,认为第二个参数应该排在前面 + // 返回0的时候,认为无所谓谁放前面 + public static class IdShengAgeJiangOrder implements Comparator { + // 根据id从小到大,但是如果id一样,按照年龄从大到小 @Override public int compare(Student o1, Student o2) { - return o2.id - o1.id; + return o1.id != o2.id ? (o1.id - o2.id) : (o2.age - o1.age); } } - public static class AgeAscendingComparator implements Comparator { + public static class IdAscendingComparator implements Comparator { + // 返回负数的时候,第一个参数排在前面 + // 返回正数的时候,第二个参数排在前面 + // 返回0的时候,谁在前面无所谓 @Override public int compare(Student o1, Student o2) { - return o1.age - o2.age; + return o1.id - o2.id; } } - public static class AgeDescendingComparator implements Comparator { + public static class IdDescendingComparator implements Comparator { @Override public int compare(Student o1, Student o2) { - return o2.age - o1.age; + return o2.id - o1.id; } } - - - public static class AgeShengIdSheng implements Comparator { - - @Override - public int compare(Student o1, Student o2) { - return o1.age != o2.age ? (o1.age - o2.age) - : (o1.id - o2.id); - } - } - - // 先按照id排序,id小的,放前面; // id一样,age大的,前面; public static class IdInAgeDe implements Comparator { @Override public int compare(Student o1, Student o2) { - return o1.id != o2.id ? o1.id - o2.id : ( o2.age - o1.age ); + return o1.id != o2.id ? o1.id - o2.id : (o2.age - o1.age); } } - public static void printStudents(Student[] students) { for (Student student : students) { @@ -108,105 +92,78 @@ public class Code01_Comparator { } } - - - public static class AComp implements Comparator{ + + public static class AComp implements Comparator { // 如果返回负数,认为第一个参数应该拍在前面 // 如果返回正数,认为第二个参数应该拍在前面 // 如果返回0,认为谁放前面都行 @Override public int compare(Integer arg0, Integer arg1) { - + return arg1 - arg0; - + // return 0; } - + } - public static void main(String[] args) { - - Integer[] arr = {5,4,3,2,7,9,1,0}; - + + Integer[] arr = { 5, 4, 3, 2, 7, 9, 1, 0 }; + Arrays.sort(arr, new AComp()); - - for(int i = 0 ;i < arr.length;i++) { + + for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } - - System.out.println("==========================="); - - Student student1 = new Student("A", 2, 20); - Student student2 = new Student("B", 3, 21); - Student student3 = new Student("C", 1, 22); - Student[] students = new Student[] { student1, student2, student3 }; - System.out.println("第一条打印"); - - - Arrays.sort(students, new IdAscendingComparator()); - - - printStudents(students); System.out.println("==========================="); - - - Arrays.sort(students, new IdDescendingComparator()); - printStudents(students); - System.out.println("==========================="); + Student student1 = new Student("A", 4, 40); + Student student2 = new Student("B", 4, 21); + Student student3 = new Student("C", 3, 12); + Student student4 = new Student("D", 3, 62); + Student student5 = new Student("E", 3, 42); + // D E C A B - Arrays.sort(students, new AgeAscendingComparator()); - printStudents(students); - System.out.println("==========================="); -//// -//// Arrays.sort(students, new AgeDescendingComparator()); -//// printStudents(students); -//// System.out.println("==========================="); -// -// Arrays.sort(students, new AgeShengIdSheng()); -// printStudents(students); -// -// System.out.println("==========================="); -// System.out.println("==========================="); -// System.out.println("==========================="); -// -// PriorityQueue maxHeapBasedAge = new PriorityQueue<>(new AgeDescendingComparator()); -// maxHeapBasedAge.add(student1); -// maxHeapBasedAge.add(student2); -// maxHeapBasedAge.add(student3); -// while (!maxHeapBasedAge.isEmpty()) { -// Student student = maxHeapBasedAge.poll(); -// System.out.println("Name : " + student.name + ", Id : " + student.id + ", Age : " + student.age); -// } -// System.out.println("==========================="); - - PriorityQueue minHeapBasedId - = new PriorityQueue<>(new AgeAscendingComparator()); - minHeapBasedId.add(student1); - minHeapBasedId.add(student2); - minHeapBasedId.add(student3); - while (!minHeapBasedId.isEmpty()) { - Student student = minHeapBasedId.poll(); - System.out.println("Name : " + student.name + ", Id : " + student.id + ", Age : " + student.age); - } - System.out.println("==========================="); - System.out.println("==========================="); - System.out.println("==========================="); - - TreeSet treeAgeDescending = new TreeSet<>(new AgeAscendingComparator()); - treeAgeDescending.add(student1); - treeAgeDescending.add(student2); - treeAgeDescending.add(student3); + Student[] students = new Student[] { student1, student2, student3, student4, student5 }; + System.out.println("第一条打印"); - Student studentFirst = treeAgeDescending.first(); - System.out.println("Name : " + studentFirst.name + ", Id : " + studentFirst.id + ", Age : " + studentFirst.age); + Arrays.sort(students, new IdShengAgeJiangOrder()); + for (int i = 0; i < students.length; i++) { + Student s = students[i]; + System.out.println(s.name + "," + s.id + "," + s.age); + } - Student studentLast = treeAgeDescending.last(); - System.out.println("Name : " + studentLast.name + ", Id : " + studentLast.id + ", Age : " + studentLast.age); - System.out.println("==========================="); + System.out.println("第二条打印"); + ArrayList studentList = new ArrayList<>(); + studentList.add(student1); + studentList.add(student2); + studentList.add(student3); + studentList.add(student4); + studentList.add(student5); + studentList.sort(new IdShengAgeJiangOrder()); + for (int i = 0; i < studentList.size(); i++) { + Student s = studentList.get(i); + System.out.println(s.name + "," + s.id + "," + s.age); + } + // N * logN + System.out.println("第三条打印"); + student1 = new Student("A", 4, 40); + student2 = new Student("B", 4, 21); + student3 = new Student("C", 4, 12); + student4 = new Student("D", 4, 62); + student5 = new Student("E", 4, 42); + TreeMap treeMap = new TreeMap<>((a, b) -> (a.id - b.id)); + treeMap.put(student1, "我是学生1,我的名字叫A"); + treeMap.put(student2, "我是学生2,我的名字叫B"); + treeMap.put(student3, "我是学生3,我的名字叫C"); + treeMap.put(student4, "我是学生4,我的名字叫D"); + treeMap.put(student5, "我是学生5,我的名字叫E"); + for (Student s : treeMap.keySet()) { + System.out.println(s.name + "," + s.id + "," + s.age); + } } diff --git a/src/class04_06/Code02_Heap.java b/src/class04_06/Code02_Heap.java index 07ef952..270c5b6 100644 --- a/src/class04_06/Code02_Heap.java +++ b/src/class04_06/Code02_Heap.java @@ -1,5 +1,8 @@ package class04_06; +import java.util.Comparator; +import java.util.PriorityQueue; + public class Code02_Heap { public static class MyMaxHeap { @@ -39,29 +42,34 @@ public class Code02_Heap { return ans; } + + + + + + // 新加进来的数,现在停在了index位置,请依次往上移动, + // 移动到0位置,或者干不掉自己的父亲了,停! private void heapInsert(int[] arr, int index) { - // arr[index] - // arr[index] 不比 arr[index父]大了 , 停 - // index = 0; + // [index] [index-1]/2 + // index == 0 while (arr[index] > arr[(index - 1) / 2]) { swap(arr, index, (index - 1) / 2); index = (index - 1) / 2; } } - // 从index位置,往下看,不断的下沉, - // 停:我的孩子都不再比我大;已经没孩子了 + // 从index位置,往下看,不断的下沉 + // 停:较大的孩子都不再比index位置的数大;已经没孩子了 private void heapify(int[] arr, int index, int heapSize) { int left = index * 2 + 1; - while (left < heapSize) { - // 左右两个孩子中,谁大,谁把自己的下标给largest - // 右 -> 1) 有右孩子 && 2)右孩子的值比左孩子大才行 - // 否则,左 + while (left < heapSize) { // 如果有左孩子,有没有右孩子,可能有可能没有! + // 把较大孩子的下标,给largest int largest = left + 1 < heapSize && arr[left + 1] > arr[left] ? left + 1 : left; largest = arr[largest] > arr[index] ? largest : index; if (largest == index) { break; } + // index和较大孩子,要互换 swap(arr, largest, index); index = largest; left = index * 2 + 1; @@ -116,7 +124,42 @@ public class Code02_Heap { } + + public static class MyComparator implements Comparator{ + + @Override + public int compare(Integer o1, Integer o2) { + return o2 - o1; + } + + } + public static void main(String[] args) { + // 小根堆 + PriorityQueue heap = new PriorityQueue<>(new MyComparator()); + heap.add(5); + heap.add(5); + heap.add(5); + heap.add(3); + // 5 , 3 + System.out.println(heap.peek()); + heap.add(7); + heap.add(0); + heap.add(7); + heap.add(0); + heap.add(7); + heap.add(0); + System.out.println(heap.peek()); + while(!heap.isEmpty()) { + System.out.println(heap.poll()); + } + + + + + + + int value = 1000; int limit = 100; int testTimes = 1000000; diff --git a/src/class04_06/Code03_HeapSort.java b/src/class04_06/Code03_HeapSort.java index f3d8655..7a255bd 100644 --- a/src/class04_06/Code03_HeapSort.java +++ b/src/class04_06/Code03_HeapSort.java @@ -14,6 +14,7 @@ public class Code03_HeapSort { // for (int i = 0; i < arr.length; i++) { // O(N) // heapInsert(arr, i); // O(logN) // } + // O(N) for (int i = arr.length - 1; i >= 0; i--) { heapify(arr, i, arr.length); }