20162316刘诚昊 2017-2018-2 《Java程序设计》10月9日“查找课堂测试”
测试要求:
1 用JDB或IDEA单步跟踪在下列数据中(3 8 12 34 54 84 91 110)查找45和54的过程,对比使用顺序查找和二分查找的执行过程
2提交测试找到或找不到那一步的截图,要全屏,包含自己的学号信息
3课下把代码推送到代码托管平台
过程:
1.书上的代码
public class Searching {
public static Comparable linearSearch (Comparable[] data, Comparable target) {
Comparable result = null;
int index = 0;
while (result == null && index < data.length)
{
if (data[index].compareTo(target) == 0)
result = data[index];
index++;
}
return result;
}
public static Comparable binarySearch (Comparable[] data, Comparable target){
Comparable result = null;
int first = 0, last = data.length - 1, mid;
while (result == null && first <= last)
{
mid = (first + last) /2;
if (data[mid].compareTo(target) == 0)
result = data[mid];
else
if (data[mid].compareTo(target)>0)
last = mid - 1;
else
first = mid +1;
}
return result;
}
}
2.自己写一个简单的main方法并调用上面的类
public class Week5_LinearSearch {
public static void main(String[] args) {
Comparable[] num = {3, 8, 12, 34, 54, 84, 91, 110, 2316};
Searching qw = new Searching();
System.out.println(qw.linearSearch(num,54));
System.out.println(qw.linearSearch(num,45));
System.out.println(qw.binarySearch(num,54));
System.out.println(qw.binarySearch(num,45));
}
}
3.设定好节点以后用debug单步追踪得到