java 12:数组的搜索——线性查找及二分法查找

Searching is the process of looking for a specific element in an array—for example, discover-
ing whether a certain score is included in a list of scores. Searching is a common task in com-
puter programming. Many algorithms and data structures are devoted to searching. This

section discusses two commonly used approaches, linear search and binary search.

所以我们今天说的就是两种常见的对数组搜索特定的元素的方法——线性搜索及二分法搜索

1 无序数组——线性搜索

The linear search approach compares the key element key sequentially with each element in
the array. It continues to do so until the key matches an element in the array or the array is
exhausted without a match being found. If a match is made, the linear search returns the index
of the element in the array that matches the key. If no match is found, the search returns -1.

线性搜索也就是对数组进行遍历,在遍历过程中比较要寻找的数值与当前的元素,如果两者相等,则返回该index,否则遍历完还没找到就返回-1;

public class LinearSearch
{
	public static int linearSearch(int [] a,int key)
	{
		for(int i=0;i<a.length;i++)
		{
			if(key==a[i]) return i;
		}
		return -1;
		
	}	
}
线性搜索的数组可以是任意的数组,无论是否有序。他的平均搜索次数是数组长度的一半,所以,线性搜索虽然简单粗暴,但是不适合用于大数组中,也就是长度很长的数组,因为这样平均搜索时间随着长度增长而增加

2 有序数组——二分法搜索

Binary search is the other common search approach for a list of values. For binary search to
work, the elements in the array must already be ordered. Assume that the array is in ascend-
ing order. The binary search first compares the key with the element in the middle of the array.
Consider the following three cases:
■ If the key is less than the middle element, you need to continue to search for the key
only in the first half of the array.
■ If the key is equal to the middle element, the search ends with a match.
■ If the key is greater than the middle element, you need to continue to search for the
key only in the second half of the array.

二分法是在对付有序数组时候很有利的一个工具,二分查找法首先将key与数组中间的元素比较,这样数组就分为了左右两半。

1)如果key 小于该元素,那么我们只需要将key在左半部分中查找

2)如果key 大于该元素,那么我们只需要将key在有半部分中查找

3)如果key等于该元素,我们直接返回

所以,在二分查找中,我们每一次比较就能淘汰掉一半的元素,加入我们有n个元素,那么第一次比较后剩下n/2,第二次比较剩下n/2/2 ,第k次比较就剩n/2k个 ,那么当k=Log2n时候,就只剩下一个元素,那么也就是说最坏的情况下我们只需要做Log2n+1次比较就可以找到,对于一个有1024个元素的数组,最坏最坏情况也只需要比较11次。

The portion of the array being searched shrinks by half after each comparison. Let low and
high denote, respectively, the first index and last index of the array that is currently being
searched. Initially, low is 0 and high is list.length–1. Let mid denote the index of the
middle element. So mid is (low + high)/2. 

public class BinarySearch
{
	public static int binarySearch(int [] a,int key)
	{
		int low=0;
		int high=a.lenth-1;
		
		while(low<=high)
		{
			int mid=(low+high)/2;
			if(key==a[mid]) return mid;
			if(key<a[mid])
			{
				high=mid-1;	
			}else 
			{
				low=mid+1
			}
		}
		<span style="color:#ff0000;">return -low-1;</span>
		
	}
}
要注意这里如果没找到我们没有返回-1 而是返回-low-1;这是因为当我们没有找到时候,low的位置是该元素要插入的位置,但是为什么不返回-low呢? 因为如果我们key比所有的元素都小,那么最后一次比较是停留在index=0的位置,这时候low=0,那么我们返回给调用者的值就是0了,而不是一个负数,这样我们拿到0并不知道他是没匹配到,还是该元素就在第一个元素。所以我们要保证如果找不到就返回一个负数。所以就多减个1 这样-1就表示没找到并且该元素必须插到0的位置上。测试一下:

public static void main(String [] args)
	{
		int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79};
		int i = BinarySearch.binarySearch(list, 2); // Returns 0
		int j = BinarySearch.binarySearch(list, 11); // Returns 4
		int k = BinarySearch.binarySearch(list, 12); // Returns –6
		int l = BinarySearch.binarySearch(list, 1); // Returns –1
		int m = BinarySearch.binarySearch(list, 3); // Returns –2
		System.out.println("i="+i+",j="+j+",k="+k+",l="+l+",m="+m);
	}
总结:线性查找用于无序的小数组中查找是非常好的,但是对于大型的数组就非常的低效了;而对于已经排好序的数组,二分查找是非常高校的一种方法








  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值