Skip to content

Commit fd25f78

Browse files
author
tianbin
committed
左程云算法新手班 day1
1 parent 284a95c commit fd25f78

File tree

187 files changed

+812
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+812
-229
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[数据结构](src/main/java/data_struct/README.md)
44

55
# 操作系统
6-
[jvm](src/main/java/org/tianbin/jvm)
6+
[jvm](src/main/java/cc/tianbin/jvm)
77

88
# 网络编程
9-
[org.tianbin.netty](src/main/java/org/tianbin/netty/README.md)
9+
[org.tianbin.netty](src/main/java/cc/tianbin/netty/README.md)

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@
7777
<artifactId>junit</artifactId>
7878
<version>4.13.2</version>
7979
</dependency>
80+
<dependency>
81+
<groupId>org.slf4j</groupId>
82+
<artifactId>slf4j-simple</artifactId>
83+
<version>1.7.36</version>
84+
</dependency>
8085

8186
</dependencies>
8287

src/main/java/algorithm_practice/a_Sorting/a_Selection.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
*/
1616
public class a_Selection {
1717
public static void main(String[] args) {
18-
int[] a = new int[10];
19-
a = SysRandom.random(a);
18+
int[] a = SysRandom.randomArr();
2019
SysOut.printArray(a);
2120

22-
a = Selection_Sort(a);
21+
Selection_Sort(a);
2322
SysOut.printArray(a);
2423
}
2524

src/main/java/algorithm_practice/a_Sorting/b_Insertion.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
*/
1010
public class b_Insertion {
1111
public static void main(String[] args) {
12-
int[] a = new int[10];
13-
a = SysRandom.random(a);
12+
int[] a = SysRandom.randomArr();
1413
SysOut.printArray(a);
1514

1615
a = Insertion_Sort(a);

src/main/java/algorithm_practice/a_Sorting/d_Merge.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
public class d_Merge {
1111

1212
public static void main(String[] args) {
13-
int[] a = new int[7];
14-
a = SysRandom.random(a);
13+
int[] a = SysRandom.randomArr();
1514
SysOut.printArray(a);
1615

1716
divide(a, 0, a.length - 1);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 跟着左神写算法
2+
<https://github.com/algorithmzuo>
3+
4+
笔记:<https://www.yuque.com/nibnait/algorithm>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# 新手班
2+
<https://github.com/algorithmzuo/algorithm-primary>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package algorithm_practice.algorithmzuo.a_primary.class01;
2+
3+
import common.util.SystemUtil;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.junit.Test;
6+
7+
import java.math.BigDecimal;
8+
9+
/**
10+
* Created by nibnait on 2022/06/20
11+
*/
12+
@Slf4j
13+
public class Code01_PrintBinary {
14+
15+
@Test
16+
public void testCase() {
17+
18+
int num = 4;
19+
print(num);
20+
print(0);
21+
22+
print(Integer.MAX_VALUE);
23+
print(Integer.MIN_VALUE);
24+
print(Integer.MIN_VALUE + 1);
25+
print(Integer.MIN_VALUE + 2);
26+
print(Integer.MIN_VALUE + 3);
27+
print(Integer.MIN_VALUE + 4);
28+
print(Integer.MIN_VALUE + 5);
29+
30+
print(-1);
31+
32+
print(BigDecimal.valueOf(Math.pow(2, 32)).intValue());
33+
print(BigDecimal.valueOf(Math.pow(2, 31)).intValue());
34+
print(BigDecimal.valueOf(Math.pow(2, 30)).intValue());
35+
36+
SystemUtil.printCuttingLine();
37+
38+
// 带符号右移 和 不带符号右移
39+
num = 1024;
40+
print(num >> 1);
41+
print(num >>> 1);
42+
SystemUtil.printCuttingLine();
43+
44+
// 为什么负数的二进制要取反加1?
45+
// 为了加法 正+正 与 正+负。。走一套逻辑。
46+
num = 5;
47+
print(num);
48+
print(-num);
49+
print(~num + 1);
50+
}
51+
52+
private void print(int num) {
53+
System.out.print(num + "\t");
54+
for (int i = 31; i >= 0; i--) {
55+
System.out.print( (num & (1 << i)) == 0 ? "0" : "1");
56+
}
57+
System.out.println();
58+
}
59+
60+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package algorithm_practice.algorithmzuo.a_primary.class01;
2+
3+
public class Code02_SumOfFactorial {
4+
5+
public static long f1(int N) {
6+
long ans = 0;
7+
for (int i = 1; i <= N; i++) {
8+
ans += factorial(i);
9+
}
10+
return ans;
11+
}
12+
13+
public static long factorial(int N) {
14+
long ans = 1;
15+
for (int i = 1; i <= N; i++) {
16+
ans *= i;
17+
}
18+
return ans;
19+
}
20+
21+
public static long f2(int N) {
22+
long ans = 0;
23+
long cur = 1;
24+
for (int i = 1; i <= N; i++) {
25+
cur = cur * i;
26+
ans += cur;
27+
}
28+
return ans;
29+
}
30+
31+
public static void main(String[] args) {
32+
int N = 10;
33+
System.out.println(f1(N));
34+
System.out.println(f2(N));
35+
}
36+
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package algorithm_practice.algorithmzuo.a_primary.class01;
2+
3+
import common.util.CompareUtils;
4+
import common.util.SysOut;
5+
import common.util.SysRandom;
6+
import common.util.SystemUtil;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import static common.util.SwapUtil.swap;
11+
12+
/**
13+
* Created by nibnait on 2022/06/21
14+
*/
15+
public class Code03_SelectionSort {
16+
17+
@Test
18+
public void testCase() {
19+
for (int i = 0; i < 10; i++) {
20+
int arr[] = SysRandom.randomArr();
21+
SysOut.printArray(arr);
22+
selectSort(arr);
23+
SysOut.printArray(arr);
24+
Assert.assertTrue(CompareUtils.isSortAsc(arr));
25+
26+
SystemUtil.printLiteCuttingLine();
27+
}
28+
}
29+
30+
private void selectSort(int[] arr) {
31+
if (arr == null || arr.length < 2) {
32+
return;
33+
}
34+
35+
int length = arr.length;
36+
for (int i = 0; i < length - 1; i++) {
37+
int minValueIndex = i;
38+
for (int j = i+1; j < length; j++) {
39+
minValueIndex = arr[j] < arr[minValueIndex] ? j : minValueIndex;
40+
}
41+
swap(arr, i, minValueIndex);
42+
}
43+
44+
}
45+
46+
}

0 commit comments

Comments
 (0)