Algorithms L2 Searching Algorithms
Algorithms L2 Searching Algorithms
algorithms
Computer Science
Unit 6
Algorithms
2
Objectives
• Understand and use different types of search
• Binary search
• Linear search
Searching algorithms
Unit 5 Algorithms
What is searching?
• We search for things many
times a day
• Physical things
• clothes
• homework
• Virtual things
• songs
• pictures
Searching algorithms
Unit 5 Algorithms
Searching a list
• Searching is a very common operation in computing
• A doctor might search for a patient’s notes
• A policeman can instantly find who is the owner of an
abandoned car
• Think of some other large databases
that often need to be searched
Searching algorithms
Unit 5 Algorithms
A binary search
• Here is a list of names:
Ali Ben Carl Joe Ken Lara Mo Oli Pam Tara Stan
A binary search
• The middle item in the list is Lara
Ali Ben Carl Joe Ken Lara Mo Oli Pam Tara Stan
A binary search
• Here is a list of names:
Ali Ben Carl Joe Ken Lara Mo Oli Pam Tara Stan
A binary search
• Here is a list of names:
Ali Ben Carl Joe Ken Lara Mo Oli Pam Tara Stan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Worksheet 2
• Do the questions in Task 1
Searching algorithms
Unit 5 Algorithms
Linear search
• If the list to be searched is not sorted, it is not
possible to do a binary search
• A linear search may be carried out instead
Comparison of searches
• In a list of 1,000,000 items, how many items have to
be examined to establish that an item is NOT in the
list?
Searching algorithms
Unit 5 Algorithms
Comparison of searches
• With a binary search, only 20 items have to be
examined to discover that an item is not in the list!
• That’s because 1,000,000 is less than 220 but greater
than 219
• Are there any more efficient algorithms for searching an
unsorted list?
Searching algorithms
Unit 5 Algorithms
Plenary
• There are just two algorithms for searching a list that
you need to know
• linear search
• binary search
Copyright
This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you
only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the
materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.
Licence agreement
This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets,
PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to
you by PG Online Limited for use under the terms of the licence.
The materials distributed with this unit may be freely copied and used by members of a single institution on a
single site only. You are not permitted to share in any way any of the materials or part of the materials with any
third party, including users on another site or individuals who are members of a separate institution. You
acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may
be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any
third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.