Kablow!

Searching algorithms: linear and binary search

Apprends en jouant

Réponds à ces questions pour gagner de l'énergie, puis pêche et explore. Sans compte.

Pour les enseignants : diapos de leçon, notes de révision prêts à l'emploi pour Searching algorithms: linear and binary search (Computer Science, Algorithms) — utilise-les en cours, ou lance le thème en activité de classe interactive que tes apprenants jouent en direct.

Notes de leçon

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.

Diapos

Sign up free to view the lesson slides

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

Questions d'entraînement

Aperçu gratuit — 8 sur 33 questions. Inscris-toi pour toutes les voir.
  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

Crée un compte gratuit pour voir toutes les questions, les diapos, les cartes mémo et les notes de révision de ce thème.

Annales

Les annales d'entraînement pour ce thème arrivent bientôt.
Bientôt disponible