2/3的二分检索

/**
    将数组分成1/3和2/3大小不等的两部分的二分检索算法
    <==算法复杂度分析=====

        最坏情况(可以利用判定树来求):(2/3)^time = n , time = O(log2/3 n);
        最好情况:O(1);
    ======================>
*/

#include<cstdio>
#define NOTFOUND 555555555

//二分检索都需要排好序的数组
int sortedArr[] = {5,7,9,10,11,12,18,22,28};

int myBinary_search(int arr[],int s,int t,int target);

int main()
{
    //输入你要检索的目标值
    int target;
    printf("please input the target you want to find:\n");
    scanf("%d",&target);

    //开始检索并且返回结果
    int index = myBinary_search(sortedArr,0,8,target);

    //根据检索结果来打印内容
    if(index != NOTFOUND){
         printf("The index of the target number in the array is %d\n",index);
    }else{
        printf("The target is not in the array\n");
    }
    return 0;

}

//  这是一个自定义的二分检索函数
//  arr   表示待检索的数组
//  s     表示检索开始位置
//  t     表示检索结束位置
int myBinary_search(int arr[],int s,int t,int target){
    while(s<t){
         //三分之二处
        int m = (t-s)/3*2+s;
        if(arr[m] == target)
            return m;
        else if(arr[m] < target)
            s = m + 1;
        else
            t = m - 1;
    }
    return NOTFOUND;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值