zoukankan      html  css  js  c++  java
  • [数据结构与算法] : 二分查找

     1 #include <stdio.h>
     2 
     3 #define NotFound -1;
     4 typedef int ElementType;
     5 
     6 int BinarySearch( const ElementType A[], ElementType X, int N )
     7 {
     8     int Low, Mid, High;
     9 
    10     Low = 0; High = N-1;
    11     while( Low <= High ) // 注意终止条件
    12     {
    13         Mid = (Low + High) / 2;
    14         if( A[Mid] < X )
    15             Low = Mid + 1;
    16         else if( A[Mid] > X )
    17             High = Mid - 1;
    18         else
    19             return Mid;
    20     }
    21     return NotFound;
    22 }
    23 
    24 int main()
    25 {
    26     int arr[10] = {1, 2, 3, 5, 6, 7, 9, 10, 12, 15};
    27     int sizeofA = sizeof(arr) / sizeof(arr[0]);
    28     printf("BinarySearch returns %d
    ", BinarySearch(arr, 9, sizeofA));
    29     return 0;
    30 }
  • 相关阅读:
    ebs R12 支持IE11
    reloc: Permission denied
    3.23考试小记
    3.21考试小记
    3.20考试小记
    3.17考试小记
    3.15考试小记
    3.13考试小记
    3.12考试小记
    3.10考试小记
  • 原文地址:https://www.cnblogs.com/moon1992/p/7500047.html
Copyright © 2011-2022 走看看