-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tianbin
committed
Aug 10, 2022
1 parent
e585618
commit cfcc2e8
Showing
16 changed files
with
400 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/algorithm_practice/algorithmzuo/b_体系学习班/class04/Code01_MergeSort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package algorithm_practice.algorithmzuo.b_体系学习班.class04; | ||
|
||
import common.CommonConstants; | ||
import common.util.CompareUtils; | ||
import common.util.SysOut; | ||
import common.util.SysRandom; | ||
import common.util.SystemUtil; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by nibnait on 2022/07/07 | ||
*/ | ||
public class Code01_MergeSort { | ||
|
||
@Test | ||
public void testCase() { | ||
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT_1000; i++) { | ||
int arr[] = SysRandom.randomNaturalNumArr(); | ||
// int arr[] = SysRandom.randomArr(); | ||
SysOut.printArray(arr); | ||
// recursiveMergeSort(arr); | ||
mergeSort(arr); | ||
SysOut.printArray(arr); | ||
if (!CompareUtils.isSortAsc(arr)) { | ||
SysOut.println("。。。有问题❗️❗️❗️"); | ||
Assert.assertTrue(false); | ||
} | ||
|
||
SystemUtil.printLiteCuttingLine(); | ||
} | ||
} | ||
|
||
/** | ||
* 归并排序 非递归 | ||
*/ | ||
private void mergeSort(int[] arr) { | ||
if (arr == null || arr.length < 2) { | ||
return; | ||
} | ||
|
||
int mergeSize = 1; | ||
int length = arr.length; | ||
while (mergeSize < length) { | ||
|
||
// 按照步长,开始 merge | ||
int left = 0; | ||
while (left < length) { | ||
if (length - left < mergeSize) { | ||
// 此时已经不存在右边了。不需要再merge了 | ||
break; | ||
} | ||
|
||
int mid = left + mergeSize - 1; | ||
int right = mid + Math.min(mergeSize, length - mid - 1); | ||
|
||
merge(arr, left, mid, right); | ||
left = right + 1; | ||
} | ||
|
||
// 防止 mergeSize * 2 之后,导致 Integer 溢出 | ||
if (mergeSize > length / 2) { | ||
break; | ||
} | ||
mergeSize <<= 1; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 归并排序 递归版 | ||
*/ | ||
private void recursiveMergeSort(int[] arr) { | ||
if (arr == null || arr.length < 2) { | ||
return; | ||
} | ||
|
||
process(arr, 0, arr.length - 1); | ||
} | ||
|
||
private void process(int[] arr, int left, int right) { | ||
if (left == right) { | ||
return; | ||
} | ||
|
||
int mid = (left + right) / 2; | ||
process(arr, left, mid); | ||
process(arr, mid + 1, right); | ||
merge(arr, left, mid, right); | ||
} | ||
|
||
private void merge(int[] arr, int left, int mid, int right) { | ||
int p1 = left; | ||
int p2 = mid + 1; | ||
|
||
int[] help = new int[arr.length]; | ||
for (int i = left; i <= right; i++) { | ||
help[i] = arr[i]; | ||
} | ||
|
||
int index = left; | ||
while (p1 <= mid && p2 <= right) { | ||
arr[index++] = help[p1] < help[p2] ? help[p1++] : help[p2++]; | ||
} | ||
|
||
while (p1 <= mid) { | ||
arr[index++] = help[p1++]; | ||
} | ||
|
||
while (p2 <= right) { | ||
arr[index++] = help[p2++]; | ||
} | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/algorithm_practice/algorithmzuo/b_体系学习班/class04/Code02_SmallSum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package algorithm_practice.algorithmzuo.b_体系学习班.class04; | ||
|
||
import common.CommonConstants; | ||
import common.util.SysOut; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by nibnait on 2022/07/18 | ||
*/ | ||
public class Code02_SmallSum { | ||
|
||
@Test | ||
public void loopTestCase() { | ||
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT_1000; i++) { | ||
testCase(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCase() { | ||
int arr[] = new int[]{1,3,4,2,5}; | ||
int smallSum = mergeSort(arr); | ||
SysOut.println(smallSum); | ||
SysOut.printArray(arr); | ||
} | ||
|
||
private int mergeSort(int[] arr) { | ||
if (arr == null || arr.length < 2) { | ||
return 0; | ||
} | ||
|
||
return process(arr, 0, arr.length - 1); | ||
} | ||
|
||
private int process(int[] arr, int left, int right) { | ||
if (left == right) { | ||
return 0; | ||
} | ||
|
||
|
||
int mid = (left + right) / 2; | ||
return process(arr, left, mid) | ||
+ process(arr, mid + 1, right) | ||
+ merge(arr, left, mid, right); | ||
} | ||
|
||
private int merge(int[] arr, int left, int mid, int right) { | ||
int[] help = new int[arr.length]; | ||
for (int i = left; i <= right; i++) { | ||
help[i] = arr[i]; | ||
} | ||
|
||
int p1 = left; | ||
int p2 = mid + 1; | ||
int index = left; | ||
int smallSum = 0; | ||
while (p1 <= mid && p2 <= right) { | ||
if (help[p1] < help[p2]) { | ||
smallSum += (right - p2 + 1) * arr[p1]; | ||
arr[index++] = help[p1++]; | ||
} else { | ||
arr[index++] = help[p2++]; | ||
} | ||
} | ||
|
||
while (p1 <= mid) { | ||
arr[index++] = help[p1++]; | ||
} | ||
|
||
while (p2 <= right) { | ||
arr[index++] = help[p2++]; | ||
} | ||
|
||
return smallSum; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/algorithm_practice/algorithmzuo/b_体系学习班/class04/Code03_ReversePair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package algorithm_practice.algorithmzuo.b_体系学习班.class04; | ||
|
||
import common.CommonConstants; | ||
import common.util.SysOut; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by nibnait on 2022/07/21 | ||
*/ | ||
public class Code03_ReversePair { | ||
|
||
@Test | ||
public void loopTestCase() { | ||
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT_1000; i++) { | ||
testCase(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCase() { | ||
int arr[] = new int[]{1,3,4,2,5}; | ||
int smallSum = mergeSort(arr); | ||
SysOut.println(smallSum); | ||
SysOut.printArray(arr); | ||
} | ||
|
||
private int mergeSort(int[] arr) { | ||
|
||
|
||
|
||
return 0; | ||
} | ||
|
||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...ain/java/algorithm_practice/algorithmzuo/b_体系学习班/class04/Code04_BiggerThanRightTwice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package algorithm_practice.algorithmzuo.b_体系学习班.class04; | ||
|
||
import common.CommonConstants; | ||
import common.util.SysOut; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by nibnait on 2022/07/21 | ||
*/ | ||
public class Code04_BiggerThanRightTwice { | ||
|
||
@Test | ||
public void loopTestCase() { | ||
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT_1000; i++) { | ||
testCase(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCase() { | ||
int arr[] = new int[]{1,3,4,2,5}; | ||
int smallSum = mergeSort(arr); | ||
SysOut.println(smallSum); | ||
SysOut.printArray(arr); | ||
} | ||
|
||
private int mergeSort(int[] arr) { | ||
return 0; | ||
} | ||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/algorithm_practice/algorithmzuo/b_体系学习班/class05/Code01_CountOfRangeSum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package algorithm_practice.algorithmzuo.b_体系学习班.class05; | ||
|
||
import common.CommonConstants; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by nibnait on 2022/07/21 | ||
*/ | ||
public class Code01_CountOfRangeSum { | ||
|
||
@Test | ||
public void loopTestCase() { | ||
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT_1000; i++) { | ||
testCase(); | ||
} | ||
} | ||
|
||
/** | ||
* https://leetcode.cn/problems/count-of-range-sum/ | ||
*/ | ||
@Test | ||
public void testCase() { | ||
|
||
|
||
|
||
} | ||
|
||
private int countRangeSum(int[] nums, int lower, int upper) { | ||
|
||
return 0; | ||
} | ||
|
||
} |
Oops, something went wrong.