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

Chapter 10 - Searching

Uploaded by

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

Chapter 10 - Searching

Uploaded by

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

Chapter - 10

SEARCHING

Searching is a process used to find the location of a target among a list of objects.

The algorithm used to search a list depends to a large extent on the structure of the list.

There are two basic searches for arrays

1) Linear Search

2) Binary Search

The sequential search is used whenever the list is not ordered. Generally we will use this
technique only for small lists or lists that are not searched often. In the other cases we should
first sort the list and then search it using the binary search.

SEQUENTIAL SEARCH

Suppose A is a linear array with n elements. Given no other information about array A , the most
intuitive way to search for a given ITEM in array A is to compare ITEM with each element of
the array A one by one. This method, which traverses array A sequentially to locate ITEM is
called linear search or sequential search.

Algorithm Linearsearch(A,N,ITEM,LOC)

1)// Insert item at the end of array A.

Set A[N + 1] := ITEM;

2) // Intialize counter

Set LOC := 1;

3) // Search for ITEM

Repeat while A[LOC] != ITEM

Prepared by Dr.GOPI, Asst. Professor, KIST, Kakutur, Nellore, A.P Page 1


{

Set LOC := LOC + 1;

} // END OF LOOP

4) // Search successful ?

If LOC = N + 1 then set LOC := 0

5) EXIT.

BINARY SEARCH

The binary search essentially eliminates half of the array elements from the search with each
comparison. Requires a sorted array.

Method:

1) If the element being searched for is less than the middle element, then the lower half of the
array is searched.

2) If the element being searched for is greater than the middle element, then the upper half of the
array is searched.

3) If the element being searched for is equal to the middle element, then the index is returned.

Algorithm Binarysearch(A, LB, UB, ITEM, LOC)

1) // Intialize segment variables

Set BEG := LB;

END := UB;

MID := INT((BEG + MID)) / 2);

2) Repeat Steps 3 and 4 while BEG <= END

.AND. A[MID] != ITEM

Prepared by Dr.GOPI, Asst. Professor, KIST, Kakutur, Nellore, A.P Page 2


{

3) If ITEM < A[MID] then

Set END := MID – 1

else

Set BEG := MID + 1;

} // END OF IF STRUCTURE //

4) Set MID := INT(BEG + END)) / 2);

} // END OF Step 2 loop //

5) IF A[MID] = ITEM then

Set LOC := MID;

Else

Set LOC := NULL;

} // END OF IF STRUCTURE //

6) EXIT

Let A be the following sorted 13-element array :

A : 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99. Apply the binary search for finding different
values of ITEM.

a) Item = 40.

1) Initially, BEG = 1 and END = 13 , MID = INT[ ( 1 + 13) / 2] = 7 and so A[MID] = 55.

Prepared by Dr.GOPI, Asst. Professor, KIST, Kakutur, Nellore, A.P Page 3


2) Since 40 < 55, END has its value changed by END = MID – 1 = 7 – 1 = 6. Hence

MID = INT[( 1 + 6) / 2] = 3 and so A[MID] = 30.

3) Since 40 > 30, BEG has its value changed by BEG = MID + 1 = 3 + 1 = 4.

Hence MID = INT[(4 + 6) / 2] = 5 and so A[MID] = 40.

We have found ITEM in location LOC = MID = 5.The search is successful.

b) Item = 85.

Intially BEG = 1, END = 13, MID = 7 and A[MID] = 55.

2) Since 85 > 55. BEG has its value changed by BEG = MID + 1 = 7 + 1 = 8. Hence

MID = INT[( 8 + 13) / 2 )] = 10 and so A[MID] = 77.

3) Since 85 > 77 , BEG has its value changed by BEG = MID + 1 = 10 + 1 = 11. Hence

MID = INT[( 11 + 13) / 2 )] = 12 and so A[MID] = 88.

4) Since 85 < 88, END has its value changed by

END = MID – 1 = 12 – 1 = 11. Hence

MID = INT[( 11 + 11) / 2] = 11 and so A[MID] = 80.

Observe that now BEG = END = MID = 11.

5) Since 85 > 80, BEG has its value changed by BEG = MID + 1 = 12. But now BEG > END.

Hence ITEM does not belong to the array A. Hence the search is Unsuccessful.

Time complexity

The running time for the worst case is equal to O(logn).

The running time for the average case is approximately equal to worst case running time which is
O(logn).

Prepared by Dr.GOPI, Asst. Professor, KIST, Kakutur, Nellore, A.P Page 4


Linear search vs Binary search

1) Binary search is much faster than linear search for most data sets. If you look at each item in
order, you may have to look at every item in the data set before you find the one you are looking
for.

2) With binary search, you eliminate half of the data with each decision. If there are n items, then
after the first decision you eliminate n/2 of them. After the second decision you've eliminated
3n/4 of them. After the third decision you've eliminated 7n/8 of them. etc. In other words, binary
search is O(logn). You can see that for a large data set, binary search would be much better than
linear search.

INDEXED SEQUENTIAL SEARCH

In this searching method, first of all, an index file is created, that contains some specific group or
division of required record when the index is obtained, then the partial indexing takes less time
cause it is located in a specified group.

When the user makes a request for specific records it will find that index group first where that
specific record is recorded.

Characteristics of Indexed Sequential Search

 In Indexed Sequential Search a sorted index is set aside in addition to the array.
 Each element in the index points to a block of elements in the array or another expanded
index.
 The index is searched 1st then the array and guides the search in the array.
 Indexed Sequential Search actually does the indexing multiple time, like creating the
index of an index.

Prepared by Dr.GOPI, Asst. Professor, KIST, Kakutur, Nellore, A.P Page 5


The advantage of indexed sequential method is that items in the table can be examined
sequentially if all records in the file have to be accessed, yet the search time for particular item is
reduced. A sequential search is performed on smaller index rather than on the larger table. Once
the index position is found, search is made on the record table itself.

Insertion into an indexed sequential table may be difficult as there may not be any place between
two table entries which may lead to a shift to a larger number of elements.

Deletion from an indexed sequential table can be made easily by flagging deleted entries. The
item is deleted from the original table. The deleted items can be overwritten.

Prepared by Dr.GOPI, Asst. Professor, KIST, Kakutur, Nellore, A.P Page 6

You might also like