Date post: | 06-Apr-2018 |
Category: |
Documents |
Upload: | fatin-nadia |
View: | 230 times |
Download: | 0 times |
of 31
8/2/2019 3. Lecture Notes1
1/31
1
Types of Searches
Week 1
8/2/2019 3. Lecture Notes1
2/31
2
Types of searches
Linear search algorithm
Indexed sequential search algorithm
Binary search algorithm
8/2/2019 3. Lecture Notes1
3/31
3
Linear Search Algorithm
Check each item of data in turn to seeif it satisfies your criterion.
No restrictions on the data, it will work
even if the data are not ordered butmost inefficient.
If the item is not there checked every
item of data.Search through the order until we findthe value
8/2/2019 3. Lecture Notes1
4/31
4
Linear Search Algorithm
Example
How to find the name of a person?
- given their telephone number- by searching the telephonedirectory
- given their I.C number
8/2/2019 3. Lecture Notes1
5/31
5
Linear Search Algorithm
10 7 1 3 4 2 20
Let search for the number 3.
We start at the beginning and check the firstelement in the array. Is the first value 3?
No, not it. Is it the next element?
10 7 1 3 4 2 20
Is the second value 3?
No, there either. The next element?
8/2/2019 3. Lecture Notes1
6/31
6
Linear Search Algorithm
Is the third value 3?
No, there either. Next?
Is the fourth value 3?We found it.
Note: We go through each element, inorder, until we find the correct value.
8/2/2019 3. Lecture Notes1
7/31
7
I-spy game
Work in pairs.
One person thinks of a word and tells the otherplayers its first letter:
I spy with my little eye something beginning with
TThe partner then list the things they can see thatstart 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 playershave tried every possibility they can think of.
8/2/2019 3. Lecture Notes1
8/31
8
Write out the algorithm for linearsearch.
Example : Searching through a pile ofbooks.
To find a book in a pile do thefollowing:
1. Put down any books you happento be holding.
2. Put your finger against the topbook.
8/2/2019 3. Lecture Notes1
9/31
9
3. while you are not holding a bookand your finger is not at the
bottom of the pile do the following
repeatedly,if your finger is against the bookyou want
then take that book from the pileelse move your finger to the nextbook.
8/2/2019 3. Lecture Notes1
10/31
10
How often have you searched forsomething in one place after another,only to have the thing you were
looking for in the very last place youcould possibly have looked?
Its worst case you have to check
everything.
8/2/2019 3. Lecture Notes1
11/31
11
It is usually worth ordering the data ina way that suits your requirements.
Often the same set of data will be
ordered in different ways to facilitatedifferent types of search request.
Example :
- A library catalogue is ordered bothaccording to author and according tobook title.
8/2/2019 3. Lecture Notes1
12/31
12
If the data are ordered, there are twoalgorithms can be consider using:
i) Indexed sequential search algorithm
- sorted into files and follow the
sequence
ii) Binary search algorithm
- decisions with two choices
8/2/2019 3. Lecture Notes1
13/31
13
Indexed sequential search algorithm
The data are first ordered and then subdivided.
An extra list or index is then created containing thefirst or last item in each subdivision.
Method: Used in dictionary.
The index is positioned at the top right-handcorner of the page.
To find a given word, you first leaf through thepages, looking at the index, to locate the page that
the word is on. Then carry out a linear search onthe selected page.
8/2/2019 3. Lecture Notes1
14/31
14
Indexed sequential search algorithm
A set of data held on a computer system create a sub-list.
Example : To find the town name in UK by a list of telephonearea codes.- the sub-list of the telephone area codes contain the
position of the first town whose name began with A, B, C,..etc.- Thus to find York, the sub-list would first be search to findY.- This would give the position from which to start the linearsearch of the main data.
8/2/2019 3. Lecture Notes1
15/31
15
Binary-search algorithm
A method for carrying out a search.
A search algorithm for searching a setof sorted data for a particular value.
Applied to search a list of names inalphabetical order or a list of numbersin ascending order.
8/2/2019 3. Lecture Notes1
16/31
16
Binary search algorithm
The data are first sorted into ascendingorder. The following steps are then carriedout:Step 1 : Look at the middle item.
If this is the required item, thesearch is finished.If not, the item is in either thetop or bottom half: decide which
half by comparison with themiddle item.Step 2 : Apply step 1 to the chosen half.
8/2/2019 3. Lecture Notes1
17/31
17
Example
a) Find the name Robinson in the list below.b) Find the name Davis in the list below.
1 Bennett2 Blackstock
3 Brown4 Ebenezer5 Fowler6 Laing
7 Leung8 Robinson9 Salado10 Scadding
8/2/2019 3. Lecture Notes1
18/31
18
Solution
The middle name is [(1+10)/2] =[5.5]= 6 Laing
Robinson is after Ling, so the listreduces to
7 Leung
8 Robinson9 Salado
10 Scadding
8/2/2019 3. Lecture Notes1
19/31
19
The middle name is [(7+10)/2] = [8.5] = 9 Saludo
Robinson is before Saludo, so the list
reduces to7 Leung
8 Robinson
The middle name is [7+8]/2] = [7.5]
= 8 Robinsonthe search is complete and Robinson hasbeen found.
8/2/2019 3. Lecture Notes1
20/31
20
Try to do question (b)
8/2/2019 3. Lecture Notes1
21/31
21
Example : Find S in the words;
HERTFORDSHIRE
- Number the data.
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
8/2/2019 3. Lecture Notes1
22/31
22
Find the middle of the data.
1 + 13 = 7
27 is for R and not the answer.
- find the middle of 7 and 13
7 + 13 = 10 for H; not the answer2
8/2/2019 3. Lecture Notes1
23/31
23
H E R T F O R D S H I R E
1 2 3 4 5 6 7 8 9 1011 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.
8/2/2019 3. Lecture Notes1
24/31
24
Decide what to search
Number the data
Find the middle or the midpoint of thedata (take the nearest integers)
Find the data and eliminate half of thedata
Repeat
8/2/2019 3. Lecture Notes1
25/31
25
Try this
Use the binary search algorithm to
locate the name Harry in the followinglist.
Daffy, Mickey, Goody, Sonic, Donald,Daisy, Sylvester, Harry, Winnie,Tigger, Minnie, Chip.
8/2/2019 3. Lecture Notes1
26/31
26
Guessing Number
A player has to guess a positive integer selectedby another player between 1 and N, using onlyquestions 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)
8/2/2019 3. Lecture Notes1
27/31
27
At each step, we choose a numberright in the middle of the range ofpossible values for the number. For
example, once we know the numberis greater than 8, but less than orequal to 12, we know to choose a
number in the middle of the range [9,12] (either 10 or 11 will do).
8/2/2019 3. Lecture Notes1
28/31
28
Game of 20 Questions
One person thinking of a famousperson.
The other player has to work out it isjust by asking Yes or No question.
Try to do it in as few questions aspossible.
If you take more than 20 questionsyou lose.
8/2/2019 3. Lecture Notes1
29/31
29
Tutorial
Group work
i) Carry out the activities given andactivity 1.3 pg 22.
ii) Discuss your strategies andcompare to the others group.
Individual
Exercise 1E pg 28 29 no 15, 17
8/2/2019 3. Lecture Notes1
30/31
30
Activity 1
Working in pairs, tell your partner to think of awhole number between 1 and 99(inclusive). Thenby asking suitable questions to which the answermust be either yes or no, try to discover the
number. The aim is to find the number by askingas few questions as possible. Swap over and letyour partner try to find a number that you arethinking of. Repeat the exercise several times,trying different strategies. Discuss your strategies.
What is the minimum, maximum and averagenumber of questions required?
8/2/2019 3. Lecture Notes1
31/31
31
Activity 2
Use a dictionary or telephonedirectory to investigate how manyitems of data you look at when
searching for a given word orsomeones phone number. Compare
various strategies and try to describe
them as algorithms.