过程一:
打书上的代码
'''
public class SortClass {
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; // determine midpoint
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;
}
}
'''
过程二:
创建一个main方法调用上面的类
'''
/**
-
Created by 神国七律 on 2017/10/9.
*/
public class SortClassTest {
public static void main(String[] args){Comparable [] arry = {3,8,12,34,54,84,91,110,2312}; SortClass qb = new SortClass(); System.out.println(SortClass.linearSearch(arry, 45)); System.out.println(SortClass.linearSearch(arry, 54)); System.out.println(SortClass.binarySearch(arry, 45)); System.out.println(SortClass.binarySearch(arry, 54));
}
}
'''
过程三:
设定节点单步追踪