Kablow!

Searching algorithms: linear and binary search

边玩边学

回答这些题目来赚取能量,然后钓鱼探索。无需账号。

给教育者: 面向 Searching algorithms: linear and binary search(Computer Science,Algorithms)的即用型课程幻灯片, 复习笔记——用在你的课堂上,或将该知识点作为学习者可实时游玩的互动课堂活动来运行。

课程笔记

Linear search

  • A linear search checks each item in a list one by one from the start until it finds the target or reaches the end.
  • It works on unsorted or sorted data, and is simple. In the worst case every item is checked, so the time grows with the list size.

Linear search

Linear search

Binary search

  • A binary search works only on sorted data. It looks at the middle item: if it equals the target, done; if the target is smaller, discard the upper half; if larger, discard the lower half. Repeat on the remaining half.
  • It is much faster for large lists because it halves the search area each time. For 1,000 items it needs at most about 10 comparisons.
  • Maintain low and high index bounds. If the target is not found and low becomes greater than high, report not found. State how the middle position is rounded when the range has an even number of items.

Binary search

Binary search

Comparing

  • For n items, linear search needs up to n target-versus-item checks; binary search repeatedly halves a sorted range. Both can succeed on their first check, so compare worst cases rather than claiming binary is always faster.

Comparing search performance

Comparing search performance

Tracing a binary search

  • List: 2, 5, 8, 12, 16, 23, 38. Target 23: middle = 12 (4th) → target larger → right half: 16, 23, 38 → middle 23 → found.

Choosing

  • Use binary search for large sorted lists; linear search for small or unsorted lists.

幻灯片

Sign up free to view the lesson slides

Step through every slide for this topic — plus flashcards and revision notes — with a free account.

练习题

免费预览——33 题中的 8 题。注册即可查看全部。
  1. 1.What is a search algorithm?

    Easy
    • Aa method of finding an item in a list
    • Ba way of sorting
    • Ca type of loop
    • Da database
  2. 2.What does a linear search do?

    Easy
    • Achecks items one by one
    • Bchecks the middle
    • Csorts the list
    • Dcompresses data
  3. 3.What must be true for binary search?

    Easy
    • Athe list is sorted
    • Bthe list is unsorted
    • Cthe list is short
    • Dthe list is numeric only
  4. 4.What does binary search check first?

    Easy
    • Athe middle item
    • Bthe first item
    • Cthe last item
    • Da random item
  5. 5.Which search is simpler to program?

    Easy
    • Alinear
    • Bbinary
    • Cboth equal
    • Dneither
  6. 6.A list is 3, 7, 9, 14, 20. Linear search for 14 takes how many comparisons?

    Easy
    • A4
    • B1
    • C3
    • D5
  7. 7.A list is 3, 7, 9, 14, 20. Binary search for 14: what is the first item checked?

    Easy
    • A9
    • B3
    • C20
    • D14
  8. 8.After checking 9 for target 14 in the list 3, 7, 9, 14, 20, which items remain?

    Easy
    • A14 and 20
    • B3 and 7
    • C7 and 9
    • D9 only

Unlock all 33 questions & more

创建免费账号,即可查看该知识点的每一道题、幻灯片、闪卡和复习笔记。

历年真题

该知识点的历年真题练习即将推出。
即将推出