Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardforcel committed Apr 13, 2018
1 parent f80f38a commit 5e0189f
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions a.md
Expand Up @@ -150,6 +150,12 @@ _搜索_ (search)算法,接受一个集合以及一个目标项,并判断该

如果元素在序列中是排序好的,你可以用 _二分搜素_ (bisection search) ,它的增长级别是 `O(logn)` 。 二分搜索和你在字典中查找一个单词的算法类似(这里是指真正的字典,不是数据结构)。 你不会从头开始并按顺序检查每个项,而是从中间的项开始并检查你要查找的单词在前面还是后面。 如果它出现在前面,那么你搜索序列的前半部分。否则你搜索后一半。如论如何,你将剩余的项数分为一半。

练习 3

编写一个叫做`bisection`的函数,它接受有序列表和目标值,并返回列表中值的索引(如果存在的话);如果不存在则返回`None`

或者你可以阅读对分模块的文档并使用它!

如果序列有 1,000,000 项,它将花 20 步找到该单词或判断出其不在序列中。因此它比线性搜索快大概 50,000 倍。

二分搜索比线性搜索快很多,但是它要求已排序的序列,因此使用时需要做额外的工作。
Expand Down

0 comments on commit 5e0189f

Please sign in to comment.