Algorithms T3 Searching and Sorting
Algorithms T3 Searching and Sorting
OCR sorting
A Level
Computer Science Unit 8
Paper 1 Algorithms
3
Objectives
• Know and be able to trace and analyse the time
complexity of the linear search and binary search
algorithms
• Be able to trace and analyse the time complexity of
the binary tree search algorithm
• Know and be able to explain and trace and analyse
the time complexity of the bubble sort algorithm
• Be able to trace and analyse the time complexity of
the merge sort algorithm
Sorting and searching
Unit 8 Algorithms
Searching algorithms
• Searching for a particular item in a list or a database
is a very common operation in computing
• The cards list the top ten most popular girls’ names
in England in 2015, and the number of babies given
each name
Searching algorithms
• Using the cards, you can try
out two searching
algorithms
• Start by putting the cards
face down in a line in
random order
• How many babies were named
Lily in 2015?
Sorting and searching
Unit 8 Algorithms
Linear search
• The only systematic way of finding out is to look at
each card, starting with the first card, until you find
Lily
• How many cards did you have to turn up?
• If there are n names in a list, what is the average
number of names that will have to be examined?
• What is the “worst case scenario”?
Sorting and searching
Unit 8 Algorithms
Bubble sort
• You can sort the name cards manually using a
bubble sort, which you have probably already met
n = len(names)
FOR i = 0 to n - 2
FOR j = 0 to(n – i - 2)
IF names[j] > names[j + 1]
Swap the names
ENDIF
ENDFOR
ENDFOR
• A binary search is much more efficient, but the items
in the list must be sorted
Sorting and searching
Unit 8 Algorithms
Binary search
• The binary search is a very efficient way of
searching a sorted list
• Examine the middle item in the list
• If this is the one you are searching for, return the index
• Eliminate half the list, depending on whether the item being
sought is greater than or less than the middle item
• Repeat until the item is found or is proved to be not in the list
Sorting and searching
Unit 8 Algorithms
Worksheet 3
• Try the activities and questions in Task 1
Sorting and searching
Unit 8 Algorithms
Worksheet 3
• Try manually merging the unsorted list of names
• Complete Task 2
Plenary
• The binary search is a very efficient algorithm for
searching a sorted list
• You should be able to trace its execution
• There are many different sorting algorithms, with
different time complexities
• You need to be able to trace the algorithms for a
simple sort
• You should be able to describe the general principle
of the merge sort
Sorting and searching
Unit 8 Algorithms
visualization
• https://www.hackerearth.com/practice/algorithms/s
orting/bubble-sort/visualize/
Sorting and searching
Unit 8 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.