• JS实现二分查找


    代码实现

    // 思路
    // 递归--代码逻辑更加清晰
    // 非递归=--性能更好(相对来说)
    // 时间复杂度o(log(n))---很快
    // 凡有序,必二分
    // 凡二分,时间复杂度必包含O(logn)
    
    /**
     * 二分查找(循环)
     * @param arr arr
     * @param target target
     */
    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((startIndex + endIndex) / 2)
            const midValue = arr[midIndex]
            if (target < midValue) {
                // 目标值较小,则继续在左侧查找
                endIndex = midIndex - 1
            } else if (target > midValue) {
                // 目标值较大,则继续在右侧查找
                startIndex = midIndex + 1
            } else {
                // 相等,返回
                return midIndex
            }
        }
    
        return -1
    }
    
    /**
     * 二分查找(递归)
     * @param arr arr
     * @param target target
     * @param startIndex start index
     * @param endIndex end index
     */
    export function binarySearch2(arr: number[], target: number, startIndex?: number, endIndex?: number): number {
        const length = arr.length
        if (length === 0) return -1
    
        // 开始和结束的范围
        if (startIndex == null) startIndex = 0
        if (endIndex == null) 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
        }
    }
    
    // // // 功能测试
    // const arr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
    // const target = 40
    // // console.info(binarySearch2(arr, target))
    
    // // 性能测试 时间复杂度都是 O(log(n))
    // console.time('binarySearch1')
    // for (let i = 0; i < 100 * 10000; i++) {
    //     binarySearch1(arr, target)
    // }
    // console.timeEnd('binarySearch1') // 17ms
    // console.time('binarySearch2')
    // for (let i = 0; i < 100 * 10000; i++) {
    //     binarySearch2(arr, target)
    // }
    // console.timeEnd('binarySearch2') // 34ms

    单测

    /**
     * @description 二分查找 test
     */
    
    import { binarySearch1, binarySearch2 } from './binary-search'
    
    describe('二分查找', () => {
        it('正常情况', () => {
            const arr = [10, 20, 30, 40, 50]
            const target = 40
            const index = binarySearch1(arr, target)
            expect(index).toBe(3)
        })
    
        it('空数组', () => {
            expect(binarySearch1([], 100)).toBe(-1)
        })
    
        it('找不到 target', () => {
            const arr = [10, 20, 30, 40, 50]
            const target = 400
            const index = binarySearch1(arr, target)
            expect(index).toBe(-1)
        })
    })
  • 相关阅读:
    Ecshop后台管理增加成本价和毛利润统计功能
    如何在Ecshop首页调用积分商城里的商品
    Ecshop调用促销商品
    Python 精选内置函数
    Python爬虫原理
    android中解析后台返回的json字符串
    JSONObject与JSONArray的使用
    python的字符串截取
    centos6系统下网卡bond模式介绍
    fio工具中的iodepth参数与numjobs参数-对测试结果的影响
  • 原文地址:https://www.cnblogs.com/lxl0419/p/16201124.html
Copyright © 2020-2023  润新知