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\n", BinarySearch(arr, 9, sizeofA));
29     return 0;
30 }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2021-07-17
  • 2021-10-10
  • 2021-10-26
  • 2022-02-07
  • 2022-02-07
猜你喜欢
  • 2021-11-30
  • 2021-09-18
  • 2021-06-07
  • 2022-01-01
  • 2021-08-19
  • 2021-10-20
  • 2022-12-23
相关资源
相似解决方案