0% found this document useful (0 votes)
51 views

Types of Searches: Week 1

This document discusses different types of search algorithms: 1. Linear search - Checking each item sequentially until the target is found, with no restrictions on data order. It works but is inefficient. 2. Indexed sequential search - The data is ordered and divided into sections, with an index of the first item in each. Binary search is then used within sections. 3. Binary search - For sorted data, the target is checked against the middle item to determine in which half to search, recursively checking the middle until found. It is more efficient than linear search. The document provides examples and explanations of how to implement each type of search algorithm. It also describes related search activities and exercises to compare

Uploaded by

Fatin Nadia
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Types of Searches: Week 1

This document discusses different types of search algorithms: 1. Linear search - Checking each item sequentially until the target is found, with no restrictions on data order. It works but is inefficient. 2. Indexed sequential search - The data is ordered and divided into sections, with an index of the first item in each. Binary search is then used within sections. 3. Binary search - For sorted data, the target is checked against the middle item to determine in which half to search, recursively checking the middle until found. It is more efficient than linear search. The document provides examples and explanations of how to implement each type of search algorithm. It also describes related search activities and exercises to compare

Uploaded by

Fatin Nadia
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Types of Searches

Week 1

Types of searches
Linear search algorithm Indexed sequential search algorithm Binary search algorithm

Linear Search Algorithm


Check each item of data in turn to see if it satisfies your criterion. No restrictions on the data, it will work even if the data are not ordered but most inefficient. If the item is not there checked every item of data. Search through the order until we find the value
3

Linear Search Algorithm


Example How to find the name of a person? - given their telephone number - by searching the telephone directory - given their I.C number

Linear Search Algorithm


10 7 1 3 4 2 20

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

Is the second value 3? No, there either. The next element?


5

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, in order, until we find the correct value.
6

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

Write out the algorithm for linear search.


Example : Searching through a pile of books. To find a book in a pile do the following: 1. Put down any books you happen to be holding. 2. Put your finger against the top book.
8

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

Indexed sequential search algorithm


The data are first ordered and then subdivided. An extra list or index is then created containing the first or last item in each subdivision. Method: Used in dictionary. The index is positioned at the top right-hand corner of the page. To find a given word, you first leaf through the pages, looking at the index, to locate the page that the word is on. Then carry out a linear search on the selected page.

13

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 telephone area 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 find Y. - This would give the position from which to start the linear search of the main data.

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

Binary search algorithm


The data are first sorted into ascending order. The following steps are then carried out: Step 1 : Look at the middle item. If this is the required item, the search is finished. If not, the item is in either the top or bottom half: decide which half by comparison with the middle item. Step 2 : Apply step 1 to the chosen half.
16

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

Try to do question (b)

20

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

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

You might also like