zoukankan      html  css  js  c++  java
  • 查找算法:折半查找

         线性查找时间复杂度: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}
  • 相关阅读:
    深度学习
    !gcc !vi
    条件、循环及其他语句
    当索引行不通时
    我的排班日期
    Linux使用storcli工具查看服务器硬盘和raid组信息
    storcli64和smartctl定位硬盘的故障信息
    Shell-四剑客
    iostat
    /VAR/LOG/各个日志文件分析
  • 原文地址:https://www.cnblogs.com/liujin2012/p/3009731.html
Copyright © 2011-2022 走看看