Searching
Searching
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:-
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
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
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
i. low = mid + 1 = 2 + 1
ii. new value of mid = ( 3 + 5 ) / 2 = 4
iii. and so 1 [ mid ] = 1 [ 4 ] = 40 Result
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)