Fancy DSA Fancy DSA
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

想的更多,也要想的更远
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • Roadmap 路线
  • 算法简介
  • Sort 排序

  • Search 搜索

    • BinarySearch [二分搜索]
    • ExponentialSearch [指数搜索]
      • 介绍
      • 原理
      • 复杂度
      • 实现
      • 参考
    • FibonacciSearch [斐波那契搜索]
    • InterpolationSearch [插值搜索]
    • JumpSearch [跳跃搜索]
    • LinearSearch [线性搜索]
    • QuickSelectSearch [快速选择搜索]
    • SlidingWindow [滑窗算法]
    • StringSearch
    • TernarySearch [三元搜索]
    • UnionSearch [合并搜索]
  • Recursive 递归

  • Graph 图

  • Tree 树

  • Math 数学

  • Hash 哈希

  • String 字符串

  • BitManipulation 位操纵

  • Backtracking 回溯

  • DynamicProgramming 动态规划

  • Cache 缓存

  • Array 数组

  • Ciphers 密码学

  • Conversions 转换

  • ProjectEuler 欧拉计划

  • 其他

  • 算法
  • Search 搜索
jonsam
2022-05-01
目录

ExponentialSearch [指数搜索]

# 介绍

指数搜索用于通过跳跃指数位置(即 2 的幂)来搜索元素。试图找到一个相对较小的范围,在该范围内可使用其他有界搜索算法(例如二进制搜索)来搜索元素。

# 原理

试图找到一个比搜索的元素更大的元素,目的是为了最大程度地减少所需元素的范围。通过将其乘以 2 来增加范围,然后再次检查是否到达的元素大于要搜索的元素或数组的末尾。一旦达到这两个目的,便会跳出循环。然后,使用 startIndex range/2 和 lastIndex 执行二进制搜索 range。

# 复杂度

  • 平均时间复杂度:O(log⁡i){\displaystyle O(\log i)}O(logi)
  • 最坏时间复杂度:O(log⁡i){\displaystyle O(\log i)}O(logi)
  • 最优时间复杂度:O(1){\displaystyle O(1)}O(1)
  • 空间复杂度:迭代:O(1){\displaystyle O(1)}O(1)

# 实现

# JavaScript

/**
 * Exponential Search
 *
 * The algorithm consists of two stages. The first stage determines a
 * range in which the search key would reside if it were in the list.
 * In the second stage, a binary search is performed on this range.
 */

function binarySearch (arr, value, floor, ceiling) {
  // Middle index
  const mid = Math.floor((floor + ceiling) / 2)

  // If value is at the mid position return this position
  if (arr[mid] === value) {
    return mid
  }

  if (floor > ceiling) return -1

  // If the middle element is great than the value
  // search the left part of the array
  if (arr[mid] > value) {
    return binarySearch(arr, value, floor, mid - 1)
    // If the middle element is lower than the value
    // search the right part of the array
  } else {
    return binarySearch(arr, value, mid + 1, ceiling)
  }
}

function exponentialSearch (arr, length, value) {
  // If value is the first element of the array return this position
  if (arr[0] === value) {
    return 0
  }

  // Find range for binary search
  let i = 1
  while (i < length && arr[i] <= value) {
    i = i * 2
  }

  // Call binary search for the range found above
  return binarySearch(arr, value, i / 2, Math.min(i, length))
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

# 参考

  • Exponential search - Wikipedia (opens new window)
编辑 (opens new window)
上次更新: 2022/05/10, 23:46:22
BinarySearch [二分搜索]
FibonacciSearch [斐波那契搜索]

← BinarySearch [二分搜索] FibonacciSearch [斐波那契搜索]→

最近更新
01
0-20题解
10-31
02
本章导读
10-31
03
算法与转换:Part1
10-28
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy DSA | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式