Skip to content

Commit

Permalink
class04 归并排序
Browse files Browse the repository at this point in the history
  • Loading branch information
tianbin committed Aug 10, 2022
1 parent e585618 commit cfcc2e8
Show file tree
Hide file tree
Showing 16 changed files with 400 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import common.util.SysOut;
import common.util.SysRandom;
import common.util.SystemUtil;
import org.junit.Assert;
import org.junit.Test;

import static common.util.SwapUtil.swap;
Expand All @@ -19,12 +20,11 @@ public void testCase() {
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT; i++) {
int arr[] = SysRandom.randomArr();
SysOut.printArray(arr);
int[] tmp = CompareUtils.copyArray(arr);
selectSort(arr);
SysOut.printArray(arr);
if (!CompareUtils.isSortAsc(arr)) {
System.out.println("!!!有问题");
SysOut.printArray(tmp);
SysOut.println("。。。有问题❗️❗️❗️");
Assert.assertTrue(false);
}

SystemUtil.printLiteCuttingLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import common.util.SysOut;
import common.util.SysRandom;
import common.util.SystemUtil;
import org.junit.Assert;
import org.junit.Test;

import static common.util.SwapUtil.swap;
Expand All @@ -19,12 +20,11 @@ public void testCase() {
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT; i++) {
int arr[] = SysRandom.randomArr();
SysOut.printArray(arr);
int[] tmp = CompareUtils.copyArray(arr);
bubbleSort(arr);
SysOut.printArray(arr);
if (!CompareUtils.isSortAsc(arr)) {
System.out.println("!!!有问题");
SysOut.printArray(tmp);
SysOut.println("。。。有问题❗️❗️❗️");
Assert.assertTrue(false);
}

SystemUtil.printLiteCuttingLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import common.util.SysOut;
import common.util.SysRandom;
import common.util.SystemUtil;
import org.junit.Assert;
import org.junit.Test;

import static common.util.SwapUtil.swap;
Expand All @@ -19,13 +20,12 @@ public void testCase() {
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT; i++) {
int arr[] = SysRandom.randomArr();
SysOut.printArray(arr);
int[] tmp = CompareUtils.copyArray(arr);

insertSort(arr);
SysOut.printArray(arr);
if (!CompareUtils.isSortAsc(arr)) {
System.out.println("!!!有问题");
SysOut.printArray(tmp);
SysOut.println("。。。有问题❗️❗️❗️");
Assert.assertTrue(false);
}

SystemUtil.printLiteCuttingLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class Code01_1_ReverseListNode {

@Test
public void loolTestCase() {
public void loopTestCase() {
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT_1000; i++) {
testCase();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class Code01_2_ReverseDoubleListNode {

@Test
public void loolTestCase() {
public void loopTestCase() {
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT; i++) {
testCase();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class Code02_DeleteGivenValue {

@Test
public void loolTestCase() {
public void loopTestCase() {
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT_1000; i++) {
testCase();
}
Expand Down
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++];
}
}

}
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;
}

}
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;
}


}
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;
}


}
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;
}

}
Loading

0 comments on commit cfcc2e8

Please sign in to comment.