Vous êtes sur la page 1sur 12

Decision Maths

Search Algorithms

Search Algorithms
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
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.

Binary Search Algorithm


Used to search and locate a specific item in a

list. The list must be in alphabetical or numerical order. The algorithm hinges around the middle item of a sub list. The middle item is defined as 0.5(n1+n2) Where n1 is the first item in the sub list and n2 is the last.

Binary Search Algorithm


Locate the names below in the list shown.

a) Dixon b) Davy
1) Abbot

Iteration 1
Middle = 1 ( 1 + 11) = 6th 2 = Daniel

2) Brown 3) Carr 4) Casson 5) Cater 6) Daniel 7) Dixon 8) Domb 9) Fox 10) Gough 11) Hampton

Dixon is after Daniel

Binary Search Algorithm


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

3rd Iteration
7) Dixon 8) Domb

Binary Search Algorithm


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

Found Dixon and it is in the 7th position

Binary Search Algorithm


Locate the names a) Dixon b) Davy 1) Abbot 2) Brown 3) Carr 4) Casson 5) Cater 6) Daniel 7) Dixon 8) Domb 9) Fox 10) Gough 11) Hampton

1st Iteration
Middle = 1 ( 1 + 11) = 6th 2 = Daniel

Davy is after Daniel

Binary Search Algorithm


2nd Iteration
7) Dixon 8) Domb 9) Fox 10) Gough 11) Hampton

Middle = 1 ( 7 + 11) = 9th 2


= Fox

Davy is before Fox


Middle = 1 ( 7 + 8) = 7.5th = 8th 2 = Domb

3rd Iteration
7) Dixon 8) Domb

Davy is before Domb

Binary Search Algorithm


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

List has expired so Davy is not on the list

Binary Search Algorithm


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


Questions Locate James in the following lists

1) 2) 3) 4) 5) 6) 7) 8)

Andrea Bill Robert Peter Claire Polly Nick Caroline

9) 10) 11) 12) 13) 14)

Adam Benn James Katie Lou Pamela

Vous aimerez peut-être aussi