Types of Searches: Week 1
Types of Searches: Week 1
Week 1
Types of searches
Linear search algorithm Indexed sequential search algorithm Binary search algorithm
Let search for the number 3. We start at the beginning and check the first element in the array. Is the first value 3? No, not it. Is it the next element?
10
20
I-spy game
Work in pairs. One person thinks of a word and tells the other players its first letter: I spy with my little eye something beginning with T The partner then list the things they can see that start with the letter T: When a person names the thing the first person had thought of the search, the game stops. Otherwise the game continues until the players have tried every possibility they can think of.
7
3. while you are not holding a book and your finger is not at the bottom of the pile do the following repeatedly, if your finger is against the book you want then take that book from the pile else move your finger to the next book.
9
How often have you searched for something in one place after another, only to have the thing you were looking for in the very last place you could possibly have looked? Its worst case you have to check everything.
10
It is usually worth ordering the data in a way that suits your requirements. Often the same set of data will be ordered in different ways to facilitate different types of search request. Example : - A library catalogue is ordered both according to author and according to book title.
11
If the data are ordered, there are two algorithms can be consider using: i) Indexed sequential search algorithm - sorted into files and follow the sequence ii) Binary search algorithm - decisions with two choices
12
13
14
Binary-search algorithm
A method for carrying out a search. A search algorithm for searching a set of sorted data for a particular value. Applied to search a list of names in alphabetical order or a list of numbers in ascending order.
15
Example
a) b) Find the name Robinson in the list below. Find the name Davis in the list below. 1 Bennett 2 Blackstock 3 Brown 4 Ebenezer 5 Fowler 6 Laing 7 Leung 8 Robinson 9 Salado 10 Scadding
17
Solution
The middle name is [(1+10)/2] =[5.5] = 6 Laing Robinson is after Ling, so the list reduces to 7 Leung 8 Robinson 9 Salado 10 Scadding
18
The middle name is [(7+10)/2] = [8.5] = 9 Saludo Robinson is before Saludo, so the list reduces to 7 Leung 8 Robinson The middle name is [7+8]/2] = [7.5] = 8 Robinson the search is complete and Robinson has been found.
19
20
21
Find the middle of the data. 1 + 13 = 7 2 7 is for R and not the answer. - find the middle of 7 and 13 7 + 13 = 10 for H; not the answer 2
22
H E R T F O R D S H I R E 1 2 3 4 5 6 7 8 9 10 11 12 13 The answer should be between 7 and 10. So 7 + 10 = 8.5 this is 9, 2 9 stand for S, the search is finished.
23
Decide what to search Number the data Find the middle or the midpoint of the data (take the nearest integers) Find the data and eliminate half of the data Repeat
24
Try this Use the binary search algorithm to locate the name Harry in the following list. Daffy, Mickey, Goody, Sonic, Donald, Daisy, Sylvester, Harry, Winnie, Tigger, Minnie, Chip.
25
Guessing Number
A player has to guess a positive integer selected by another player between 1 and N, using only questions answered with yes or no. Example. Supposing N is 16 and the number 11 is selected, the game might proceed as follows. Is the number greater than 8? (Yes) Is the number greater than 12? (No) Is the number greater than 10? (Yes) Is the number greater than 11? (No)
26
At each step, we choose a number right in the middle of the range of possible values for the number. For example, once we know the number is greater than 8, but less than or equal to 12, we know to choose a number in the middle of the range [9, 12] (either 10 or 11 will do).
27
Game of 20 Questions
One person thinking of a famous person. The other player has to work out it is just by asking Yes or No question. Try to do it in as few questions as possible. If you take more than 20 questions you lose.
28
Tutorial
Group work i) Carry out the activities given and activity 1.3 pg 22. ii) Discuss your strategies and compare to the others group. Individual Exercise 1E pg 28 29 no 15, 17
29
Activity 1
Working in pairs, tell your partner to think of a whole number between 1 and 99(inclusive). Then by asking suitable questions to which the answer must be either yes or no, try to discover the number. The aim is to find the number by asking as few questions as possible. Swap over and let your partner try to find a number that you are thinking of. Repeat the exercise several times, trying different strategies. Discuss your strategies. What is the minimum, maximum and average number of questions required?
30
Activity 2
Use a dictionary or telephone directory to investigate how many items of data you look at when searching for a given word or someones phone number. Compare various strategies and try to describe them as algorithms.
31