查找算法:折半查找

     线性查找时间复杂度:O(n);

         折半无序(用快排活堆排)的时间复杂度:O(NlogN)+O(logN);

         折半有序的时间复杂度:O(logN);

 

顺序查找

    这种非常简单,就是过一下数组,一个一个的比,找到为止。

 

折半查找:

            第一: 数组必须有序,不是有序就必须让其有序,大家也知道最快的排序也是NLogN的,所以.....呜呜。

            第二: 这种查找只限于线性的顺序存储结构。



摘自:
http://www.cnblogs.com/huangxincheng/archive/2011/11/20/2256351.html

折半查找
26 ///</summary>
27 ///<param name="list"></param>
28 ///<returns></returns>
29 public static int BinarySearch(List<int> list, int key)
30 {
31 //最低线
32 int low = 0;
33
34 //最高线
35 int high = list.Count - 1;
36
37 while (low <= high)
38 {
39 //取中间值
40 var middle = (low + high) / 2;
41
42 if (list[middle] == key)
43 {
44 return middle;
45 }
46 else
47 if (list[middle] > key)
48 {
49 //下降一半
50 high = middle - 1;
51 }
52 else
53 {
54 //上升一半
55 low = middle + 1;
56 }
57 }
58 //未找到
59 return -1;
60 }
61 }


折半递归算法:

 1

摘自 http://www.cnblogs.com/iPeterRex/archive/2008/08/04/1260344.html

int binarySearch( const int b[], int searchKey, int low, int high )
 2{
 3    int middle = ( low + high ) / 2;
 4    if ( searchKey == b[ middle ] )
 5        return middle;
 6    else if ( searchKey < b[ middle ] )
 7        return binarySearch( b, searchKey, low, middle - 1 );
 8    else if ( searchKey > b[ middle ] )
 9        return binarySearch( b, searchKey, middle + 1, high );
10    else
11        return -1;
12}

转载于:https://www.cnblogs.com/liujin2012/archive/2013/04/09/3009731.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值