• 二分查找法


    #include<stdio.h>
    
    int BinarySearch(const int a[],const int t,int lengh) ///暂时没错的版本,注意middle最好不要直接赋值,因为那样如果找不到,会引起死循环
    {
      printf("%d",sizeof(a));///放到这里输出为4
      int start = 0, end = lengh - 1;
      while(start <= end)
      {
        printf("start = %d and end = %d 
    ",start,end);
        int middle = start + (end - start)/2;
        if(a[middle] == t)
            return middle;
        else if(a[middle] < t)
            start = middle + 1;
        else
            end = middle - 1;
      }
      printf("The end");
    }
    
    int BinarySearchRecuision(const int a[], const int t, int start, int end)
    {
        if(start > end)
            return -1;
        int middle = (start + end)/2;
        if(a[middle] == t)
            return middle;
        else if (t > a[middle])
            return BinarySearchRecuision(a,t,middle+1,end);
        else
            return BinarySearchRecuision(a,t,start,middle-1);
    }///这估计就是传说中的分治法吧~
    
    void main()
    {
        int a[] = {1,2,3,5}; 
        int lengh = sizeof(a)/sizeof(a[0]);
        printf("%d",sizeof(a)); ///放到这里输出为16
        BinarySearch(a,5,lengh);
    }
    berkeleysong
  • 相关阅读:
    android adb常用命令
    weblogic 优化设置 http://wenku.baidu.com/view/c42e7a5bbe23482fb4da4cf2.html
    Ajax+json实现菜单动态级联
    浏览器兼容性
    登录相关
    Java 日期处理
    QBC分页查询
    WebService
    MD5加密
    JSON与JAVA数据的转换
  • 原文地址:https://www.cnblogs.com/berkeleysong/p/3733908.html
Copyright © 2020-2023  润新知