题目要求:
- 1 用JDB或IDEA单步跟踪在下列数据中(3 8 12 34 54 84 91 110)查找45和54的过程,对比使用顺序查找和二分查找的执行过程
- 2提交测试找到或找不到那一步的截图,要全屏,包含自己的学号信息
- 3课下把代码推送到代码托管平台
实验过程:
调用Searching中的binarySearch和linearSearch方法,然后再进行强制类型转换,转为Integer类型后查找45和54,打印出45、54是否找到
关键代码:
Searching:
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by zjc on 2017/11/6.
*/
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;
}
public static int insertionSearch(int a[], int value,
int low, int high)
{
int cp = low + (value - a[low]) / (a[high] - a[low]) * (high - low);
if (a[cp] == value)
return a[cp];
if (a[cp] > value)
return insertionSearch(a, value, low, cp - 1);
if (a[cp] < value) ;
return insertionSearch(a, value, cp + 1, high);
}
final int max_size = 20;
public Integer FibonacciSearch(Integer a[], Integer key)
{
int low = 0;
int high = a.length - 1;
int[] F = new int[max_size];
F[0] = 1;
F[1] = 1;
for (int i = 2; i < max_size; ++i)
F[i] = F[i - 1] + F[i - 2];
int h = 0;
while (a.length > F[h] - 1)
h++;
Integer[] temp = new Integer[F[h] - 1];
for (int j = 0; j < a.length; j++)
temp[j] = a[j];
for (int i = a.length; i < F[h] - 1; i++)
temp[i] = a[a.length - 1];
while (low <= high)
{
int mid = low + F[h - 1] - 1;
if (key < temp[mid])
{
high = mid - 1;
h = h-1;
} else if (key > temp[mid])
{
low = mid + 1;
h = h-2;
}
else
{
if (mid < a.length)
return mid;
else
return a.length - 1;
}
}
return null;
}
public static Comparable binarySearchTree(Comparable [] data, Comparable target){
Comparable result = null;
LinkedBinarySearchTree tree = new LinkedBinarySearchTree();
for (int i = 0;i< data.length;i ++){
tree.add(data[i]);
}
if (tree.find(target) == target)
result = target;
return result;
}
public static class blockSearch {
private int[] index;
private ArrayList[] list;
public void blockSearch(int[] index, int i, int i1) {
if (index != null && index.length != 0) {
this.index = index;
this.list = new ArrayList[index.length];
for (i = 0; i < list.length; i++) {
list[i] = new ArrayList();
}
} else {
throw new Error("index cannot be null or empty");
}
}
}
public int hashSearch(int[] data, int z)
{
boolean result = false;
HashMap<Integer, Integer> cop = new HashMap();
for(int c = 0; c<data.length; c++)
cop.put(c,data[c]);
for(int i = 0; i<data.length; i++)
if (data[i]==z)
result = true;
if(result)
return z;
else
return -1;
}
}
test
/**
* Created by zjc on 2017/11/6.
*/
public class searchtest {
public static void main(String[] args) {
int[] A1 = {2312, 2, 5, 8, 95, 3, 6, 5, 4,7,3,2, 6, 7};
Integer[] A2 = {2, 2, 3, 5,4,2,6,8,435, 5, 6, 6, 7, 8, 95, 2312};
Searching searching = new Searching();
System.out.println(searching.hashSearch(A1, 6));
System.out.println(searching.FibonacciSearch(A2,95));
System.out.println(searching.insertionSearch(A1,8,2,0));
System.out.println(searching.binarySearchTree(A2,8));
}
}