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

Algorithms T3 Searching and Sorting

Uploaded by

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

Algorithms T3 Searching and Sorting

Uploaded by

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

Searching and

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

Amelia Olivia Isla Emily Poppy


5,327 4,724 4,012 3,991 3,273

Ava Isabella Jessica Lily Sophie


3,171 3,022 2,995 2,965 2,905
Sorting and searching
Unit 8 Algorithms

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

Algorithm for linear search


• Complete this algorithm:
SUB linearSearch(namelist,nameSought)
index 🡨 -1
i 🡨 0
found 🡨 False
WHILE i < length(namelist) AND NOT found
IF namelist[i] = nameSought THEN
??????
??????
ENDIF
??????
ENDWHILE
RETURN index
ENDSUB
Sorting and searching
Unit 8 Algorithms

Analysing the algorithm


• How many steps are there in the algorithm? What is
its time complexity?
SUB linearSearch(namelist,nameSought)
index 🡨 -1
i 🡨 0
found 🡨 False
WHILE i < length(namelist) AND NOT found
IF namelist[i] = nameSought THEN
index 🡨 i
found 🡨 True
ENDIF
i 🡨 i + 1
ENDWHILE
RETURN index
ENDSUB
Sorting and searching
Unit 8 Algorithms

Big-O for linear search


• There are 2 statements in the loop (an IF statement
and an assignment statement) and 3 at the start
SUB linearSearch(namelist,nameSought)
index 🡨 -1
i 🡨 0
found 🡨 False
WHILE i < length(namelist) AND NOT found
IF namelist[i] = nameSought THEN
index 🡨 i
found 🡨 True
ENDIF
i 🡨 i + 1
ENDWHILE
RETURN index
ENDSUB
• Total number of steps = 2n + 3 in worst case
• Time complexity = O(n)
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

Analysing the bubble sort


• How many statements are there in the worst case
scenario, in which a swap is made every time?
• Assume that “swap the items” counts as three
statements
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
Sorting and searching
Unit 8 Algorithms

Big-O for bubble sort


• The statements in the inner nested loop are
performed n(n-1 + n-2 + n-3 +….. 1)
• This is approximately an2 +bn statements
• Ignoring the coefficient of n2 and the term in n, the
time complexity = O(n2)
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
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

Binary tree search


• The names have been put into a binary search tree

• What is the maximum number of searches to find any name?


• What is the order of complexity?
Sorting and searching
Unit 8 Algorithms

A balanced binary tree


• Note that the tree on the
previous slide is balanced, as
each side has three levels below
the root
• An unbalanced tree would look like
the one on the right
• What effect would this have on the search
time?
• What would be the Big-O time complexity?
Sorting and searching
Unit 8 Algorithms

The merge sort


• This is much more efficient than the bubble sort
• The basic steps are:
• Divide the unsorted list into n sublists, each
containing one element
• Repeatedly merge two sublists at a time to produce
new sorted sublists until there is only one sublist
remaining. This is the sorted list.
• We will use a list of 8 integers to demonstrate the
process
Sorting and searching
Unit 8 Algorithms

Merge sort part 1


• Divide the unsorted list into n sublists
Sorting and searching
Unit 8 Algorithms

Merge sort part 2


• Now merge the pairs of sublists into a single sorted
list
Sorting and searching
Unit 8 Algorithms

Merging two lists


List A List B

• Read item from list A; Read item from list B.


• Write smaller to output list.
• Read next item from the list that held the smaller
value
• Repeat until all items written to output list
Sorting and searching
Unit 8 Algorithms

Worksheet 3
• Try manually merging the unsorted list of names
• Complete Task 2

Amelia Olivia Isla Emily Poppy


5,327 4,724 4,012 3,991 3,273

Ava Isabella Jessica Lily Sophie


3,171 3,022 2,995 2,965 2,905
Sorting and searching
Unit 8 Algorithms

Big-O for the merge sort


• This is another example of a ‘divide and conquer’
algorithm which halves the size of each list to be
merged
• First the list is split into halves, log2n times
• Each of these log2n splits requires n operations to
merge the data into a single list
• So, the time complexity is O(n log2n)
Sorting and searching
Unit 8 Algorithms

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

© 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