Skip to content

Commit c45530e

Browse files
committed
[Function add]
1.Test the machanism of java passing parameter. 2.Binary search.(Recursive)
1 parent e67f6a2 commit c45530e

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

Algorithm(4th_Edition)/algorithm_note.txt

+37-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,40 @@
1414

1515
3.计算调和级数(Harmonic series):
1616
1.证明其为发散数列。
17-
实现:ca.mcmaster.chapter.one.MyMath.harmonicSeries(int)
17+
实现:ca.mcmaster.chapter.one.MyMath.harmonicSeries(int)
18+
19+
4.Java值传递:
20+
class Test{
21+
String name;
22+
public String getName() {
23+
return name;
24+
}
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
}
29+
private static void call(Test t){
30+
Test tnew = new Test();
31+
t.setName("abc");
32+
System.out.println(t); //ca.mcmaster.chapter.one.Test@2a8b83e3
33+
tnew.setName("111");
34+
t = tnew;
35+
System.out.println(t); //ca.mcmaster.chapter.one.Test@2d7fc1e7
36+
}
37+
public static void main(String[] args) {
38+
//Java pass value test
39+
Test t = new Test();
40+
System.out.println(t); //ca.mcmaster.chapter.one.Test@2a8b83e3
41+
call(t);
42+
System.out.println(t); //ca.mcmaster.chapter.one.Test@2a8b83e3
43+
}
44+
1.基本数据类型都是值传递。
45+
2.引用类型传递是引用的拷贝,既不是引用本身,更不是对象。
46+
***因为java本身不存在地址,传参时相当于复制了当前地址的引用,并将复制的引用进行传递。
47+
48+
5. 二分法查找:
49+
1.查找的数组是顺序的。
50+
2.通过递归的方法不断调用,传入需要比较的上界和下界。
51+
3.比较中间位置的值和查找值的大小,相同结束回归,不相同修改上界和下界。
52+
实现:ca.mcmaster.chapter.one.Recursive.rank(int, int[])
53+

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/one/MyMath.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,35 @@ public static Double harmonicSeries(int N) {
2626
}
2727
return sum;
2828
}
29+
30+
private static void call(Test t){
31+
Test tnew = new Test();
32+
t.setName("abc");
33+
System.out.println(t);
34+
tnew.setName("111");
35+
t = tnew;
36+
System.out.println(t);
37+
}
2938

3039
public static void main(String[] args) {
3140
// System.out.println(isPrime(13));
3241
// System.out.println(sqrt(2D));
33-
System.out.println(harmonicSeries(1000));
42+
// System.out.println(harmonicSeries(1000));
43+
44+
//Java pass value test
45+
Test t = new Test();
46+
System.out.println(t);
47+
call(t);
48+
System.out.println(t);
49+
}
50+
}
51+
52+
class Test{
53+
String name;
54+
public String getName() {
55+
return name;
56+
}
57+
public void setName(String name) {
58+
this.name = name;
3459
}
3560
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ca.mcmaster.chapter.one;
2+
3+
public class Recursive {
4+
private static int rank(int key, int[] a){
5+
return rank(key, a, 0, a.length - 1);
6+
}
7+
8+
public static int rank(int key, int[] a, int low, int hi){
9+
System.out.println("low is " + low + "; high is " + hi);
10+
if(low > hi){
11+
return -1;
12+
}
13+
int mid = low + (hi - low)/2;
14+
if(key < a[mid])
15+
return rank(key, a, low, mid - 1);
16+
else if(key > a[mid])
17+
return rank(key, a, mid + 1, hi);
18+
else
19+
return mid;
20+
}
21+
22+
public static void main(String[] args){
23+
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9};
24+
System.out.println(rank(7, a));
25+
}
26+
}

0 commit comments

Comments
 (0)