zoukankan      html  css  js  c++  java
  • 数据结构 二分查找 递归与非递归算法

    #include <stdlib.h>
    #include <stdio.h>
    //二分查找非递归
    
    int Binary_Search(int list[],int key,int length){
        int low=0,high=length-1;
        while (low<=high){
            int mid=(high+low)/2;
            if (list[mid]==key)
                return ++mid;
            else if (list[mid]>key)
                high=mid-1;
            else if (list[mid]<key)
                low=mid+1;
        }
        return -1;
    }
    int main(){
        int list[10]={1,3,5,6,7,8,9,10,11,19};
        int number=Binary_Search(list,10,10);
        printf("%d",number);
        return 0;
    }
    
    #include <stdlib.h>
    #include <stdio.h>
    //二分查找 递归
    
    int Binary_Search(int list[],int key,int low,int high){
        int mid = (low+high)/2;
            if (list[mid]==key)
                return ++mid;
            else if (list[mid]<key)
                Binary_Search(list,key,mid+1,high);
            else if (list[mid]>key)
                Binary_Search(list,key,low,high-1);
    }
    int main(){
        int list[10]={1,3,5,6,7,8,9,10,11,19};
        int result=Binary_Search(list,10,0,10);
        printf("%d",result);
        return 0;
    }
    
  • 相关阅读:
    Spring boot 启动图片
    Spring Cloud 从入门到入门
    理解错误的 Arrays.asList()
    git github 对代码的管理
    【POJ 2154】Color
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    [数据结构]Hash Table(哈希表)
  • 原文地址:https://www.cnblogs.com/szj666/p/13406014.html
Copyright © 2011-2022 走看看