实现二分查找

用二分实现的关键点:

  • 凡有序,必二分
  • 凡二分,时间复杂度比包含O(logn)
  • 可以用递归 和 非递归 两种方式实现
    • 递归实现的二分,逻辑更清晰一些
    • 用非递归实现,性能更好一些

代码实现: 

  • 非递归实现二分法
    复制代码
    export function binarySearch1(arr:number[],target: number): number{
        const length = arr.length;
        if(length === 0) return -1
    
        let startIndex = 0;  //开始位置
        let endIndex = length -1;//结束位置
    
        while(startIndex <= endIndex){
            const midIndex = Math.floor((endIndex + startIndex) / 2)
            const midValue = arr[midIndex]
            if(target < midValue){
                endIndex = midIndex -1 
            }else if(target > midValue){
                startIndex = midIndex + 1
            }else {
                return midIndex
            }
        }
        return -1
    }
    复制代码
  • 递归实现二分法
    复制代码
    export function binarySearch2 (arr: number[],target: number, startIndex?: number,endIndex?:number){
        const length = arr.length;
        if(length === 0){
            return -1
        }
        startIndex = startIndex || 0
        endIndex = endIndex || length -1
        // 如果 start 大于 end ,则结束
        if(startIndex > endIndex){
            return -1
        }
    
        const midIndex = Math.floor((startIndex + endIndex) / 2)
        const midValue = arr[midIndex]
        if(target < midValue){ //目标较小,则继续查找
            return binarySearch2(arr,target,startIndex,midIndex -1)
        }else if(target > midValue){ //目标较大,则继续查找
            return binarySearch2(arr,target,midIndex + 1,endIndex)
        }else{
            // 相等,返回
            return midIndex
        }
    }
    复制代码
  • 测试两种实现的测试用例
    复制代码
    /**
     * @description 二分查找测试
     */
    import  {binarySearch1,binarySearch2} from './binary-search'
    
    describe('二分查找测试',() =>{ 
        it('binarySearch1正常查找', ()=>{
            const arr = [20,30,40,50,60,70]
            const res = binarySearch1(arr,60)
            expect(res).toBe(4)
        })
        it('binarySearch1空数组', ()=>{
            const arr: number[] = []
            const res = binarySearch1(arr,60)
            expect(res).toBe(-1)
        })
        it('binarySearch1不存在情况', ()=>{
            const arr = [20,30,40,50,60,70]
            const res = binarySearch1(arr,45)
            expect(res).toBe(-1)
        })
    
        it('binarySearch2正常查找', ()=>{
            const arr = [20,30,40,50,60,70]
            const res = binarySearch2(arr,60)
            expect(res).toBe(4)
        })
        it('binarySearch2空数组', ()=>{
            const arr: number[] = []
            const res = binarySearch2(arr,60)
            expect(res).toBe(-1)
        })
        it('binarySearch2不存在情况', ()=>{
            const arr = [20,30,40,50,60,70]
            const res = binarySearch2(arr,45)
            expect(res).toBe(-1)
        })
    })
    复制代码

     

     

posted @   yangkangkang  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示