Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
73 views

Algorithms L2 Searching Algorithms

Uploaded by

Shop Alice
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Algorithms L2 Searching Algorithms

Uploaded by

Shop Alice
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

IGCSE Searching

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

Searching a sorted list


• Suppose you have a dictionary and you want to look
up the word nebula
• What will be your strategy for finding the word?
• Will you start at the first word and search through till
you find it?
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

The quickest way to find if a particular name is in the


list is to do a binary search
• Suppose we are searching for the name Mo
• The list has 11 items
• Examine the middle one first
Searching algorithms
Unit 5 Algorithms

A binary search
• The middle item in the list is Lara

Ali Ben Carl Joe Ken Lara Mo Oli Pam Tara Stan

• Lara comes before Mo alphabetically so we can


discard all the names from Ali to Lara
• Now we only have 5 names to search
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

• Examine the middle name of the remaining list


• The middle name is Pam
• Mo comes before Pam so we can discard all the
names from Pam to Stan
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

• Now we only have two names


• The “middle” name is taken to be the first one
• (e.g. In a list of 6 names, the third name is the
middle one)
• Examine the middle name, Mo
• Bingo! How many names did you look at?
Searching algorithms
Unit 5 Algorithms

“Divide and conquer”

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

• In a binary search, the size of the list is


approximately halved each time an item is examined
• How many items, at most, would have to be
examined in a list of 16 items to find the one you are
looking for?
• Try looking for the number 23 in this hidden list of
numbers
• Which box will you look at first?
Searching algorithms
Unit 5 Algorithms

“Divide and conquer”


42
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

• You’re looking for the number 23


• You’ve found the number 42
• Which box will you look at next?
Searching algorithms
Unit 5 Algorithms

“Divide and conquer”


35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

• You’re looking for the number 23


• You’ve found the number 35
• Which box will you look at next?
Searching algorithms
Unit 5 Algorithms

“Divide and conquer”


27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

• You’re looking for the number 23


• You’ve found the number 27
• Which box will you look at next?
Searching algorithms
Unit 5 Algorithms

“Divide and conquer”


23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

• You’ve found the number 23!


• How many numbers did you look at?
Searching algorithms
Unit 5 Algorithms

“Divide and conquer”


23 27 32 35 37 38 41 42 45 50 52 53 54 58 61 67
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

• You looked in boxes 8, 4, 2 and 1


• In a list of 2n items, the maximum number of items
you will need to look at will be n + 1
• How many items would be examined if you were
searching for 67 instead of 23?
• Try searching for 61 in a list of 15 numbers (delete
67 from the list)
• How many items need to be examined?
Searching algorithms
Unit 5 Algorithms

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

145 27 83 777 492 588 91 678 399 123

• Items are examined in the sequence


145, 27, 83, 777….
Searching algorithms
Unit 5 Algorithms

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

• Be sure you can explain how both of these are


carried out
• (In large databases, indexes are organised and searched in
a different way which you will learn about in an A Level
course)
Searching algorithms
Unit 5 Algorithms

Copyright

© 2016 PG Online Limited

The contents of this unit are protected by 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.

You might also like