查找算法 JavaScript实现

// 顺序查找
function sequenceSearch(arr, value) {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] === value) {
            return i
        }
    }
    return -1
}
// 二分查找 递归
function binarySearch(arr, value, head, tail) {
    var len = arr.length
    head = typeof head != 'number' ? 0 : head
    tail = typeof tail != 'number' ? len - 1 : tail
    if (tail < head) {
        return -1
    }
    var middleIndex = Math.floor((tail + head) / 2)
    if (arr[middleIndex] === value) {
        return middleIndex
    }
    if (arr[middleIndex] < value) {
        return binarySearch(arr, value, middleIndex + 1, tail)
    } else {
        return binarySearch(arr, value, head, middleIndex - 1)
    }
}
// 二分查找 折半
function binarySearch1(arr, value) {
    var mid = Math.floor(arr.length / 2)
    var low = 0
    var high = arr.length
    while (low <= high) {
        console.log(mid)
        if (arr[mid] === value)
            return mid
        else if (arr[mid] < value) {
            low = mid + 1
        } else {
            high = mid - 1
        }
        mid = Math.floor((low + high) / 2)
    }
}
// 插值查找
function insertionSearch(arr, value, low, high) {
    var len = arr.length
    if (arr.length < 2) {
        if (arr[0] === value)
            return 0
        else
            return -1
    }
    low = typeof low != 'number' ? 0 : low
    high = typeof high != 'number' ? len - 1 : high
    var midIndex = low + Math.floor((value - arr[low]) / (arr[high] - arr[low]) * (high - low))
    if (arr[midIndex] === value) {
        return midIndex
    }
    else if (arr[midIndex] > value) {
        return insertionSearch(arr, value, low, midIndex - 1)
    }
    else if (arr[midIndex] < value) {
        return insertionSearch(arr, value, midIndex + 1, high)
    }
}
// 斐波那契查找
function fibonacci(n) {
    if (n < 3)
        return 1
    else
        return fibonacci(n - 1) + fibonacci(n - 2)
}
function fibonacciSearch(arr, value) {
    var len = arr.length
    var low = 0
    var high = len - 1
    var k = 1
    while (high > fibonacci(k) - 1) {
        k++
    }
    for (let i = high + 1; i < fibonacci(k) - 1; i++) {
        arr.push(arr[high])
    }
    console.log(arr)
    while (low <= high) {
        var mid = low + fibonacci(k - 1) - 1
        console.log(k + " " + low + " " + high + " " + mid)
        if (arr[mid] === value) {
            return mid
        }
        else if (value < arr[mid]) {
            high = mid - 1
            k -= 1
        }
        else {
            low = mid + 1
            k -= 2
        }
    }
    return -1
}
// console.log(fibonacciSearch(arr,48))
// 二叉树节点
function Node(data, left, right) {
    this.data = data;
    this.left = left;
    this.right = right;
}
// 二叉树
function TREE() {
    this.root = null
    this.insertNode = insertNode
}
// 插入节点
function insertNode(data) {
    let node = new Node(data, null, null)
    if (!this.root) {
        this.root = node
    } else {
        let current = this.root
        while (current) {
            if (current.data > node.data) {
                if (current.left)
                    current = current.left
                else {
                    current.left = node
                    break
                }
            }
            else {
                if (current.right)
                    current = current.right
                else {
                    current.right = node
                    break
                }
            }
        }
    }
}
var tree = new TREE
for (let i = 0; i < arr.length; i++) {
    tree.insertNode(arr[i]) //生成二叉树
}
// 最简单的树表查找算法——二叉树查找算法。
function treeSearch(tree, value) {
    if (tree.root) {
        current = tree.root
        while (current) {
            if (value > current.data) {
                current = current.right
            } else if (value < current.data) {
                current = current.left
            } else if (value === current.data) {
                return 1
            }
        }
    }
    return -1
}
console.log(treeSearch(tree,20))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值