T2 Searching Algorithms
T2 Searching Algorithms
Starter
• We search for items
many times a day
• Physical objects such as:
• Clothes
• Homework
Searching a list
• Searching is a very common operation in computing
• A doctor might search for a patient’s notes
• A police officer can search with a vehicle identification
number (VIN) to find the owner of an abandoned car
• A web browser can search for a word
in a web page
• There are numerous
other examples
of searches
Searching algorithms
Unit 6 Algorithms
A binary search
• Here is a list of names:
Ali Ben Carl Joe Ken Lara Mo Oli Pam Stan Tara
A binary search
• The middle item in the list is Lara
Ali Ben Carl Joe Ken Lara Mo Oli Pam Stan Tara
A binary search
• Here is a list of names:
Ali Ben Carl Joe Ken Lara Mo Oli Pam Stan Tara
A binary search
• Here is a list of names:
Ali Ben Carl Joe Ken Lara Mo Oli Pam Stan Tara
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Worksheet 2
• Now complete Task 1 on Worksheet 2
Searching algorithms
Unit 6 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 sorted list of 1,000,000
items, how many items
will have to be examined
to establish that an item
is NOT in the list?
Searching algorithms
Unit 6 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 2²º but greater than 2¹⁹
• If the list were unsorted, 1,000,000 items would need to be
checked to prove that the item is not in the list
• With a linear search, 1,000,000 items would need to
be examined
Searching algorithms
Unit 6 Algorithms
Worksheet 2
• Now complete Task 2 on Worksheet 2
Searching algorithms
Unit 6 Algorithms
A search algorithm
1 numbers = [5,1,9,8,7,6,4,10]
2 searchItem = int(input())
3 for i=0 to numbers.length - 1
4 if numbers[i] == searchItem:
5 print("searchItem found")
6 endif
7 endfor
• The above algorithm asks the user to input a number
and then searches for it in the array numbers
• What is the value of numbers.length – 1 ?
• When will "searchItem found" be output?
• Name the type of search algorithm
Searching algorithms
Unit 6 Algorithms
A search algorithm
1 numbers = [5,1,9,8,7,6,4,10]
2 searchItem = int(input())
3 for i=0 to numbers.length - 1
4 if numbers[i] == searchItem:
5 print("searchItem found")
6 endif
7 endfor
• Answers:
• What is the value of numbers.length – 1 ?
7
• When will "searchItem found" be output?
Each time it finds a matching item in the array
• Name the type of search algorithm
Linear search
Searching algorithms
Unit 6 Algorithms
Plenary
• With a partner explain how the following searching
algorithms work:
• Binary search
• Linear search
Plenary
• Binary search
• This only works on a sorted list
• The middle item of the list is first checked
• If the item searched for is less than this item the right of the
list is discarded, and a binary search is carried out on the left
of the list
• Linear search
• Each item in the list is checked against the search item
in order
Searching algorithms
Unit 6 Algorithms
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.