Kablow!

Searching algorithms: linear and binary search

Học bằng cách chơi

Trả lời những câu hỏi này để kiếm năng lượng, rồi câu cá và khám phá. Không cần tài khoản.

Dành cho nhà giáo dục: slide bài học, ghi chú ôn tập sẵn dùng cho Searching algorithms: linear and binary search (Computer Science, Algorithms) — dùng trong bài giảng của bạn, hoặc chạy chủ đề như một hoạt động lớp học tương tác để người học chơi như một trò chơi trực tiếp.

Ghi chú bài học

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.

Slide

Sign up free to view the lesson slides

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

Câu hỏi luyện tập

Xem trước miễn phí — 8 trên 33 câu hỏi. Đăng ký để xem tất cả.
  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

Tạo tài khoản miễn phí để xem mọi câu hỏi, slide, thẻ ghi nhớ và ghi chú ôn tập cho chủ đề này.

Đề thi cũ

Luyện đề thi cũ cho chủ đề này sắp ra mắt.
Sắp ra mắt