Skip to content

Commit 2218c6d

Browse files
committed
modify code on class
1 parent aec9feb commit 2218c6d

File tree

3 files changed

+206
-19
lines changed

3 files changed

+206
-19
lines changed

src/class02/Code01_PreSum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public RangeSum2(int[] array) {
2929
preSum = new int[N];
3030
preSum[0] = array[0];
3131
for (int i = 1; i < N; i++) {
32-
preSum[i] = preSum[i + 1] + array[i];
32+
preSum[i] = preSum[i - 1] + array[i];
3333
}
3434
}
3535

src/class02/Code02_RandToRand.java

Lines changed: 115 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,128 @@ public static int random(RandomBox randomBox, int from, int to) {
104104
}
105105

106106
public static void main(String[] args) {
107+
System.out.println("测试开始");
108+
// Math.random() -> double -> [0,1)
109+
//
110+
111+
int testTimes = 10000000;
112+
int count = 0;
113+
for (int i = 0; i < testTimes; i++) {
114+
if (Math.random() < 0.75) {
115+
count++;
116+
}
117+
}
118+
System.out.println((double) count / (double) testTimes);
119+
120+
System.out.println("=========");
121+
122+
// [0,1) -> [0,8)
123+
count = 0;
124+
for (int i = 0; i < testTimes; i++) {
125+
if (Math.random() * 8 < 5) {
126+
count++;
127+
}
128+
}
129+
System.out.println((double) count / (double) testTimes);
130+
System.out.println((double) 5 / (double) 8);
131+
132+
int K = 9;
133+
// [0,K) -> [0,8]
134+
135+
int[] counts = new int[9];
136+
for (int i = 0; i < testTimes; i++) {
137+
int ans = (int) (Math.random() * K); // [0,K-1]
138+
counts[ans]++;
139+
}
140+
for (int i = 0; i < K; i++) {
141+
System.out.println(i + "这个数,出现了 " + counts[i] + " 次");
142+
}
143+
144+
System.out.println("=========");
107145

108-
int[] count = new int[8]; // 0 1 2 .. 7
109-
int testTime = 10000000;
110-
for (int i = 0; i < testTime; i++) {
111-
int ans = c();
112-
count[ans]++;
146+
count = 0;
147+
double x = 0.17;
148+
for (int i = 0; i < testTimes; i++) {
149+
if (xToXPower2() < x) {
150+
count++;
151+
}
152+
}
153+
System.out.println((double) count / (double) testTimes);
154+
System.out.println((double) 1 - Math.pow((double) 1 - x, 2));
155+
156+
System.out.println("==========");
157+
count = 0;
158+
for (int i = 0; i < testTimes; i++) {
159+
if (f2() == 0) {
160+
count++;
161+
}
113162
}
163+
System.out.println((double) count / (double) testTimes);
114164

165+
System.out.println("==========");
166+
167+
counts = new int[8];
168+
for (int i = 0; i < testTimes; i++) {
169+
int num = g();
170+
counts[num]++;
171+
}
115172
for (int i = 0; i < 8; i++) {
116-
System.out.println(i + " : " + count[i]);
173+
System.out.println(i + "这个数,出现了 " + counts[i] + " 次");
117174
}
118175

119-
// RandomBox randomBox = new RandomBox(3, 9);
120-
// int from = 17;
121-
// int to = 29;
122-
// int[] ans = new int[to + 1];
123-
// int testTime1 = 1000000;
124-
// for (int i = 0; i < testTime1; i++) {
125-
// ans[random(randomBox, from, to)]++;
126-
// }
127-
// for (int i = 0; i < ans.length; i++) {
128-
// System.out.println(ans[i]);
129-
// }
130-
// System.out.println("==========");
176+
}
177+
178+
// 返回[0,1)的一个小数
179+
// 任意的x,x属于[0,1),[0,x)范围上的数出现概率由原来的x调整成x平方
180+
public static double xToXPower2() {
181+
return Math.min(Math.random(), Math.random());
182+
}
183+
184+
// lib里的,不能改!
185+
public static int f1() {
186+
return (int) (Math.random() * 5) + 1;
187+
}
131188

189+
// 随机机制,只能用f1,
190+
// 等概率返回0和1
191+
public static int f2() {
192+
int ans = 0;
193+
do {
194+
ans = f1();
195+
} while (ans == 3);
196+
return ans < 3 ? 0 : 1;
197+
}
198+
199+
// 得到000 ~ 111 做到等概率 0 ~ 7等概率返回一个
200+
public static int f3() {
201+
return (f2() << 2) + (f2() << 1) + f2();
202+
}
203+
204+
// 0 ~ 6等概率返回一个
205+
public static int f4() {
206+
int ans = 0;
207+
do {
208+
ans = f3();
209+
} while (ans == 7);
210+
return ans;
211+
}
212+
213+
public static int g() {
214+
return f4() + 1;
215+
}
216+
217+
// 你只能知道,x会以固定概率返回0和1,但是x的内容,你看不到!
218+
public static int x() {
219+
return Math.random() < 0.84 ? 0 : 1;
220+
}
221+
222+
// 等概率返回0和1
223+
public static int y() {
224+
int ans = 0;
225+
do {
226+
ans = x();
227+
} while (ans == x());
228+
return ans;
132229
}
133230

134231
}

src/class02/Code03_Comp.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package class02;
2+
3+
public class Code03_Comp {
4+
5+
public static void selectionSort(int[] arr) {
6+
if (arr == null || arr.length < 2) {
7+
return;
8+
}
9+
for (int i = 0; i < arr.length - 1; i++) {
10+
int minIndex = i;
11+
for (int j = i + 1; j < arr.length; j++) {
12+
if (arr[j] < arr[minIndex]) {
13+
minIndex = j;
14+
}
15+
}
16+
swap(arr, i, minIndex);
17+
}
18+
}
19+
20+
public static void swap(int[] arr, int i, int j) {
21+
int tmp = arr[i];
22+
arr[i] = arr[j];
23+
arr[j] = tmp;
24+
}
25+
26+
public static void insertionSort(int[] arr) {
27+
if (arr == null || arr.length < 2) {
28+
return;
29+
}
30+
for (int i = 1; i < arr.length; i++) { // 0 ~ i 做到有序
31+
for (int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
32+
swap(arr, j, j + 1);
33+
}
34+
}
35+
}
36+
37+
// 返回一个数组arr,arr长度[0,maxLen-1],arr中的每个值[0,maxValue-1]
38+
public static int[] lenRandomValueRandom(int maxLen, int maxValue) {
39+
int len = (int) (Math.random() * maxLen);
40+
int[] ans = new int[len];
41+
for (int i = 0; i < len; i++) {
42+
ans[i] = (int) (Math.random() * maxValue);
43+
}
44+
return ans;
45+
}
46+
47+
public static int[] copyArray(int[] arr) {
48+
int[] ans = new int[arr.length];
49+
for (int i = 0; i < arr.length; i++) {
50+
ans[i] = arr[i];
51+
}
52+
return ans;
53+
}
54+
55+
// arr1和arr2一定等长
56+
public static boolean isSorted(int[] arr) {
57+
if (arr.length < 2) {
58+
return true;
59+
}
60+
int max = arr[0];
61+
for (int i = 1; i < arr.length; i++) {
62+
if (max > arr[i]) {
63+
return false;
64+
}
65+
max = Math.max(max, arr[i]);
66+
}
67+
return true;
68+
}
69+
70+
public static void main(String[] args) {
71+
int maxLen = 5;
72+
int maxValue = 1000;
73+
int testTime = 10000;
74+
for (int i = 0; i < testTime; i++) {
75+
int[] arr1 = lenRandomValueRandom(maxLen, maxValue);
76+
int[] tmp = copyArray(arr1);
77+
selectionSort(arr1);
78+
if (!isSorted(arr1)) {
79+
for (int j = 0; j < tmp.length; j++) {
80+
System.out.print(tmp[j] + " ");
81+
}
82+
System.out.println();
83+
System.out.println("选择排序错了!");
84+
break;
85+
}
86+
}
87+
88+
}
89+
90+
}

0 commit comments

Comments
 (0)