Kablow!

Searching algorithms: linear and binary search

Aprenda jogando

Responda a estas perguntas para ganhar energia, depois pesque e explore. Sem precisar de conta.

Para educadores: slides de aula, resumos de revisão prontos para usar de Searching algorithms: linear and binary search (Computer Science, Algorithms) — use na sua aula ou rode o tópico como atividade interativa de turma que seus aprendizes jogam como um jogo ao vivo.

Notas de aula

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.

Slides

Sign up free to view the lesson slides

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

Questões de prática

Prévia grátis — 8 de 33 perguntas. Cadastre-se para ver todas.
  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

Crie uma conta grátis para ver todas as perguntas, os slides, flashcards e resumos de revisão deste tópico.

Provas anteriores

A prática com provas anteriores deste tópico chega em breve.
Em breve