zoukankan      html  css  js  c++  java
  • 折半查找&clock函数

    #include <stdio.h>
    #include <time.h>

    #define CLOCKS_PER_SEC ((clock_t)1000)

    int binsearch(int, int array[], int n);

    int main()
    {

      int array[] = {2, 5, 6, 8, 9, 11, 12, 13, 14, 15, 16};
      int tag = 9;
      int res = -1;

      clock_t start;
      clock_t finish;

      start = clock();
      res = binsearch(9, array, 11);
      finish = clock();

      printf("%ld %ld ", start, finish);
      printf("%d %f ", res, (double)(finish-start)/CLOCKS_PER_SEC);

      return 0;
    }


    int binsearch(int tag, int array[], int n)//对顺序数组,进行折半查找
    {
      int low = 0;
      int high = n-1;
      int mid = 0;

      long i = 4500000;
      while(i--);

      while(low <= high)
      {
        mid = (low+high)/2;

        if(tag > array[mid])
        {
          low = mid + 1;
        }
        else if(tag < array[mid])
        {
          high = mid - 1;
        }
        else
        {
          return mid;
        }

      }

      return -1;

    }

  • 相关阅读:
    [转载]苹果推送通知服务
    Lovekey
    大数阶乘的位数
    大明A+B
    大数取余
    A+Bcoming
    大数取余(C++)
    验证角谷猜想
    麦森数(转)
    大数阶乘的位数(C++)
  • 原文地址:https://www.cnblogs.com/zhangxuan/p/5942252.html
Copyright © 2011-2022 走看看