You are on page 1of 16

http://www.furthermaths.org.

uk

Decision Maths

Search Algorithms
Search Algorithms Wiltshire

There are many situations where we need to


search for specific things.
An example is when you look someone up in
a phone book.
You have been using an algorithm without
realising it.
We are now going to look at different search
algorithms.
Search Algorithms Wiltshire

In your pairs get one person to think of a number


between 1 and 99, keeping it secret.
The other person needs to ask questions which
have a yes or no answer to locate that number.
What is the min/max and average number of
questions that need to be answered to locate the
number?
Can you describe an efficient algorithm?
Min = 1
Max = 98
Best method is to half known range each time.
Linear Search Algorithm Wiltshire

This is the easiest of the search algorithms.


You basically search each item in the list till you find the one
that satisfies your criteria.
There is no restriction on the data and it can be in any order.
This is however the least efficient search algorithm.
Especially if the item you are searching for is not on the list.
Imagine trying to locate someones details from the phone
book if you only know their telephone number.
It is usually worth ordering the data to suit your needs.
Give some examples of sorting methods that you have seen.
How would you locate a name in the phone book.

How would you find a library book

How would you locate your name in the school


register.
Search Algorithms Wiltshire

Phone books are ordered alphabetically on


the first letter of surname then second then
third etc.
Library books can be categorised, then
ordered by the author, then the book title.
Class lists, by year, by tutor, alphabetical.
Indexed Sequential
Search Algorithm Wiltshire

The data are first ordered and then subdivided.


An extra list or index is created using the first or
last item in the subdivision
In the same way as the examples on the last
slide.
This method is used in phone books and
dictionaries.
The index appears at the top corner of the page.
If you are searching for horse in the dictionary
you first flick through the pages to locate H, then
you would perform a linear search on that page.
Binary Search Algorithm Wiltshire

A binary search works only with sorted arrays.


The search starts out by first looking at the element in
the middle of the array to be searched.
If this element is equal to the target value, the search
is over.
If it's less than the target value, the search discards
the whole first half of the array (since the array is sorted in
ascending order, if the middle value is less than the target value,
every element before it, which are less than the middle value,
must also be less than the target value and therefore are not
equal to the target).
The search then treats the second half of the array as
another array and starts the process over again: it
divides this array in half and looks at the middle
value.
Binary Search Algorithm Wiltshire

This procedure continues until the search either finds


the target value or it has looked at every location in
the array where the target value might be.
The procedure is similar if the middle value in the
array is greater than the target value. In this case, the
second half of the original array is discarded.
The binary search is generally faster than the linear
search because even if it does not find the target, it
never has to check every element in the array.
The maximum no. of comparison
Floor( log2N + 1)

# of items, N log2N Floor(log2N + 1)


127 6.99 7
Binary Search Algorithm Wiltshire

Locate the names below in the list shown.


a) Dixon
b) Davy

1) Abbot Iteration 1
2) Brown
3) Carr Middle = 1 ( 1 + 11) = 6th
4) Casson 2
5) Cater
6) Daniel
= Daniel
7) Dixon
8) Domb
9) Fox
10) Gough Dixon is after Daniel
11) Hampton
Binary Search Algorithm Wiltshire

2nd Iteration
Middle = 1 ( 7 + 11) = 9th
7) Dixon
2
8) Domb
9) Fox
= Fox
10) Gough
11) Hampton
Dixon is before Fox

3rd Iteration Middle = 1 ( 7 + 8) = 7.5th = 8th


2
7) Dixon = Domb
8) Domb
Dixon is before Domb
Binary Search Algorithm Wiltshire

4th Iteration
7) Dixon
Middle = 1 ( 7 + 7) = 7th
2

= Dixon

Found Dixon and it is in the 7th position


Binary Search Algorithm Wiltshire

Locate the names


a) Dixon 1st Iteration
b) Davy

1) Abbot Middle = 1 ( 1 + 11) = 6th


2) Brown 2
3) Carr
4) Casson = Daniel
5) Cater
6) Daniel
7) Dixon
8) Domb Davy is after Daniel
9) Fox
10) Gough
11) Hampton
Binary Search Algorithm Wiltshire

2nd Iteration
Middle = 1 ( 7 + 11) = 9th
7) Dixon
2
8) Domb
9) Fox
= Fox
10) Gough
11) Hampton
Davy is before Fox

3rd Iteration Middle = 1 ( 7 + 8) = 7.5th = 8th


2
7) Dixon = Domb
8) Domb
Davy is before Domb
Binary Search Algorithm Wiltshire

4th Iteration

7) Dixon
Middle = 1 ( 7 + 7) = 7th
2

= Dixon

List has expired so Davy is not on the list


Binary Search Algorithm Wiltshire

The Algorithm proceeds most smoothly when


the number of data items is 3, 7, 15, 31, 63.
Can you see why?
The sequence has nth term 2n-1
Each sub list is always odd and part of the
same sequence, so when you add one and
half it works perfectly.
In some cases it can be beneficial to add a
dummy item to make the list up to 2n -1.
Binary Search Algorithm Wiltshire

Questions
Locate James in the following lists
1) Andrea 1) Adam
2) Bill 2) Benn
3) Robert 3) James
4) Peter 4) Katie
5) Claire 5) Lou
6) Polly 6) Pamela
7) Nick
8) Caroline

You might also like