zoukankan      html  css  js  c++  java
  • 折半查找实现算法二(递归办法)PS:编译后有一个warning,但不影响结果,代码设计上应该还有些问题




    #include<iostream>
    using namespace std;
    #define NIL 1000;
    void main()
    {  int BinarySearch(int *A,int begin,int end ,int elem);
     int A[8]={1,3,5,7,8,10,12,14};
     int index=BinarySearch(A,0,7,10);
     cout<<index<<endl;

     char f;
     cin>>f;
    }

    int BinarySearch(int *A,int begin,int end,int elem)
    {
     int l=begin;
     int h=end;
     int mid=(l+h)/2;
     if(A[mid]==elem)
      return mid;
     else
     {
      if(A[mid]<elem)
      {
       l=mid;
       if(h-l>1)
        BinarySearch(A,l,h,elem);
       else
        return NIL;
      }
      else
      {
       h=mid;
       if(h-l>1)
        BinarySearch(A,l,h,elem);
       else
        return NIL;
      }
     }

    }



  • 相关阅读:
    python 对比学习
    支付宝
    springboot logback
    ngnix学习视频
    node学习
    puppeteer 相关知识
    Dota2App--第三天
    Dota2APP--第二天
    Dota2APP--第一天
    iOS ---进阶之摇一摇
  • 原文地址:https://www.cnblogs.com/finallyliuyu/p/1544224.html
Copyright © 2011-2022 走看看