0% found this document useful (0 votes)
38 views6 pages

Searching

serching tech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Searching

serching tech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

SEARCHING TECHNIQUES

Searching means to search an element form a vector or a key value in a table. There are
different searching techniques which are used for searching. Different techniques are used for searching
depending upon the ordering of the table. In an unordered table sequential search is useful whereas in an
ordered table Binary search is useful.

SEQUENTIAL SEARCH:-

The simplest technique for searching an unordered table for a particular record is to
scan each entry in the table in a sequential manner. Until the desired record is found. An algorithm for such
a search procedure is as follows.

In this technique the value of the key is compared with the first element of the
list(table). If match is found then search is terminated otherwise the next element from the list is fetched
and compared with key and this process till continue key is found or list completely exhausted.

Examples:
Suppose the element in table are as following
22,33,66,05,44,89
now you want to search the value the 05 so 05 is the key value , the key value
compared with each and every element and find key value when value is find then search is terminated.
Index Element Index Element
1 22 05 1 22
2 33 2 33
3 66 05 3 66
4 05 4 05
5 44 5 44
6 89 6 89

(1)
(3)
Index Element Index Element
1 22 1 22
2 33 05 2 33
3 66 3 66
4 05 05 4 05
5 44 5 44
6 89 6 89

(2) (4)
Procedure:
Function: LINEAR-SEARCH (K, N, X)

 These function searches the list K consist of N elements for the value of
X

 ALGORITHM:-
Steps:

1: [Initialize search]
i ← 1, K[n+1] ← X
2: [Search the vector]
Repeat while k[i] ≠ X
i← i+1
3: [successful search ?]
If i=N+1
Then write (‘UNSUCCESSFUL SEARCH’)
Return (0)
else write (‘SUCESSFUL SEARCH’)
Return (1)
4: Finished….
BINARY SEARCHING:-

Another relatively simple method of accessing a table is


the binary search method. The entries in the tables are stored in alphabetically or
numerically increasing order. A search for a particular item with a certain key
value resembles he search for an item. The approximate middle entry of the item
is located, and its key value is examined. If this value is too high, then the key
value of the middle entry of the first half of the table is examined and the process
is repeated on the first half until the required item is found. If the value is too low,
then the key of the middle entry of the second half of the table is tried and the
process is repeated on the second half. This process continues until the desired
key is found or the search interval becomes empty.
Example:
Binary search works for sorted lists and is a very efficient
searching technique. It is used to find the location of a given element or record in
a list. Other information associated with the elements can also be fetched if
required. To find the location of a file in the computer directory one can use this
searching technique. If we are on the internet then it is not a easy task using linear
search to find out the information about some records or files. For this one prefers
Binary searching techniques. Let low represents lower limit of the list l, high
upper and mid is average of low and high that is middle of the lower and upper, it
is

mid = ( low + high ) / 2

0 1 2 3 4 5 6 7 8 9 10 11
12
99 11 22 40 55 60 88 80 77 66 44 33 30

List in the Memory.

l [ low ] 11
l[0]
22
30 l[2]
mid = 2 33
40 l [4 ]
l[6] 44
mid = 6
55
60
66
77
80
88
99
high = 12
l [12 ]

We compare the middle element with the key to be searched. If the value of
middle element is greater than key, then will exist in lower half of the list, take high =
mid – 1 and find new value of mild,
i.e. ,
mid = ( low + high ) / 2

otherwise, key will exist in upper half and take

low = mid + 1
and mid = ( low + high ) / 2

The process continues until the entire list is exhausted or key is searched, for
example, consider the list given figure. This list contain 13 elements, i.e. , from 0 – 12,
suppose we want to search for key = 40.

This list is unsorted so that it is required to sort the list before starting the
searching. The sorted list is given in figure. How does the binary search technique work ?

1. Initially

i. l [ low ] = I [ 0 ] = 11
ii. l [ high ] = 1 [ 12 ] = 99
iii. mid = ( low + high ) / 2 ( 0 + 12 ) / 2
iv. 1 [ mid ] = 1 [ 6 ] = 55

2. Since 40 < 55 means elements < [ mid ] so that

i. high = mid – 1 (6-1=5)


ii. now new value of mid = ( 0 + 5 ) / 2 = 2
iii. so that 1 [ mid ] = 1 [ 2 ] = 30

3. Since 40 > 30 so that

i. low = mid + 1 = 2 + 1
ii. new value of mid = ( 3 + 5 ) / 2 = 4
iii. and so 1 [ mid ] = 1 [ 4 ] = 40 Result

The following algorithm performs a binary search.

Procedure:
Function BINARY-SEARCH (K, N, X)
 These function searches the list K consist of N elements for the value of
X
 LOW, MIDDLE and HIGH denote the lower, middle, and upper limits of
the search interval respectively.

 ALGORITHM:-
Steps:

1: [Initialize]
Low ← 1
HIGH ← N
2: [perform search]
Repeat thru step 4 while low ≤ HIGH
3: [Obtain index of midpoint of interval]
MIDDLE← (Low + HIGH) / 2
4: [compare]
If X < K [MIDDLE]
Then HIGH← MIDDLE-1
Else if X > k [MIDDEL]
Then write (‘SUCCESFUL SEARCH’)
Return (MIDDLE)

5: [Unsuccessful search]
Write (‘successful search’)
Return (0)

The binary-search technique has certain undesirable


properties. An insertion of a new record requires that many records in the
existing table be physically moved in order to preserve the sequential ordering.
The ration of insertion time or deletion time to search time is quite high for this
method.

You might also like