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

建立免費帳號,即可查看這個主題的所有題目、投影片、字卡與複習筆記。

歷屆試題

這個主題的歷屆試題練習即將推出。
即將推出