1、顺序查找(无序)
2、折半查找(必须是有序数组)
实现代码(java)
public class lineSearch {
//测试代码
public static void main(String[] args) {
//顺序查找
int[] arr1 = {34,76,23,98,87,35,8,32,74,44};
int key = 8;
System.out.println(key+"出现的索引为:"+search(arr1,8));//6
//二分法查找(有序数组)
int[] arr2 = {1,2,3,4,5,6,7,8,9,10};
//非递归方式
System.out.println(key+"出现的索引为:"+myBinarySearch(arr2,8));//7
//递归方式
System.out.println(key+"出现的索引为:"+myBinarySearch2(arr2,8));//7
}
/**
* 时间复杂度:T(n) = O(logn)
* 空间复杂度:S(n) = O(logn)
* 递归二分法查找
* @param arr
* @param key
* @return
*/
private static int myBinarySearch2(int[] arr, int key) {
int low = 0;
int high = arr.length;
return binarySearch(arr,key