<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Ytl's Java Blog

    厚積而薄發---每一天都是一個全新的開始
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Binary search algorithm

    Posted on 2011-05-06 18:11 ytl 閱讀(470) 評論(0)  編輯  收藏 所屬分類: Algorithms and programming concepts

    Binary search algorithm

    Generally, to find a value in unsorted array, we should look through elements of an array one by one, until searched value is found. In case of searched value is absent from array, we go through all elements. In average, complexity of such an algorithm is proportional to the length of the array.

    Situation changes significantly, when array is sorted. If we know it, random access capability can be utilized very efficientlyto find searched value quick. Cost of searching algorithm reduces to binary logarithm of the array length. For reference, log2(1 000 000) ≈ 20. It means, that in worst case, algorithm makes 20 steps to find a value in sorted array of a million elements or to say, that it doesn't present it the array.

    Algorithm

    Algorithm is quite simple. It can be done either recursively or iteratively:

    1. get the middle element;
    2. if the middle element equals to the searched value, the algorithm stops;
    3. otherwise, two cases are possible:
      • searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element.
      • searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.
    Now we should define, when iterations should stop. First case is when searched element is found. Second one is when subarray has no elements. In this case, we can conclude, that searched value doesn't present in the array.

    Examples

    Example 1. Find 6 in {-1, 5, 6, 18, 19, 25, 46, 78, 102, 114}.

    Step 1 (middle element is 19 > 6):     -1  5  6  18  19  25  46  78  102  114

    Step 2 (middle element is 5 < 6):      -1  5  6  18  19  25  46  78  102  114

    Step 3 (middle element is 6 == 6):     -1  5  6  18  19  25  46  78  102  114

    Example 2. Find 103 in {-1, 5, 6, 18, 19, 25, 46, 78, 102, 114}.

    Step 1 (middle element is 19 < 103):   -1  5  6  18  19  25  46  78  102  114

    Step 2 (middle element is 78 < 103):   -1  5  6  18  19  25  46  78  102  114

    Step 3 (middle element is 102 < 103):  -1  5  6  18  19  25  46  78  102  114

    Step 4 (middle element is 114 > 103):  -1  5  6  18  19  25  46  78  102  114

    Step 5 (searched value is absent):     -1  5  6  18  19  25  46  78  102  114

    Complexity analysis

    Huge advantage of this algorithm is that it's complexity depends on the array size logarithmically in worst case. In practice it means, that algorithm will do at most log2(n) iterations, which is a very small number even for big arrays. It can be proved very easily. Indeed, on every step the size of the searched part is reduced by half. Algorithm stops, when there are no elements to search in. Therefore, solving following inequality in whole numbers:

    n / 2iterations > 0

    resulting in

    iterations <= log2(n).

    It means, that binary search algorithm time complexity is O(log2(n)).

    Code snippets.

    You can see recursive solution for Java and iterative for python below.

    Java

    int binarySearch(int[] array, int value, int left, int right) {

          if (left > right)

                return -1;

          int middle = left + (right-left) / 2;

          if (array[middle] == value)

                return middle;

          if (array[middle] > value)

                return binarySearch(array, value, left, middle - 1);

          else

                return binarySearch(array, value, middle + 1, right);           

    }

    Python

    def biSearch(L,e,first,last):

          if last - first < 2: return L[first] == e or L[last] == e

          mid = first + (last-first)/2

          if L[mid] ==e: return True

          if L[mid]> e : 

                return biSearch(L,e,first,mid-1)

          return biSearch(L,e,mid+1,last)

          

    主站蜘蛛池模板: 亚洲国产成人久久精品影视| 亚洲午夜久久久久妓女影院| 亚洲第一页在线视频| 18禁在线无遮挡免费观看网站| 国产成人综合亚洲亚洲国产第一页| h视频免费高清在线观看| 亚洲成?v人片天堂网无码| 日本一区二区三区在线视频观看免费| 亚洲国产婷婷综合在线精品| 一区二区三区免费精品视频| 日本亚洲国产一区二区三区| 中文字幕久精品免费视频| 亚洲欧洲一区二区| 日本视频一区在线观看免费| 亚洲www在线观看| 四虎影视精品永久免费| 一个人看的www视频免费在线观看| 亚洲日韩欧洲乱码AV夜夜摸| 免费人妻无码不卡中文字幕系| 亚洲a视频在线观看| 狼友av永久网站免费观看| 污污的视频在线免费观看| 亚洲另类激情综合偷自拍图| 最近免费mv在线电影| 亚洲人成色77777在线观看| 大胆亚洲人体视频| 成人A片产无码免费视频在线观看| 亚洲视频日韩视频| 精品久久免费视频| a毛片在线免费观看| 亚洲另类图片另类电影| 免费a级毛片大学生免费观看| 在线观看肉片AV网站免费| 国产99在线|亚洲| 亚洲综合久久夜AV | 波多野结衣免费在线观看| 在线播放国产不卡免费视频| 久久精品亚洲一区二区三区浴池 | 全亚洲最新黄色特级网站| 很黄很污的网站免费| 中文字幕无码亚洲欧洲日韩|