Linear Binary Search Seminar
Linear Binary Search Seminar
Submitted by : Raj S J
Class : MCA – II
Roll number : 07
SEARCHING
LINEAR SEARCH
Suppose DATA is a linear array with n elements. Given no other
information about DATA, The most intuitive way to search for a given
ITEM in DATA is to compare ITEM with each element of DATA one by
one. That is, first we test whether DATA[1] = ITEM, and then we test
whether DATA[2] = ITEM, and so on. This method, which traverses DATA
sequentially to locate ITEM, is called linear search or sequential search.
Let us see an example below were DATA is an array with five names and
ITEM is the info whose location LOC we are searching in DATA. In this
case array DATA has 5 elements. We are searching for ITEM = “ KAREN “
Here the ITEM “ KAREN “ is checked with first element of DATA i.e. “
MARY “. Since first element of DATA is not equal to ITEM, now 2nd
element of DATA “ JANE” is checked and so on until either the array is
completely traversed or the LOC of ITEM is found. Here on the fifth
comparison the LOC of ITEM is found to be 5 in DATA.
On the other hand in order to use step 1, one must guarantee that there is an
unused memory location at the end of the array DATA; otherwise one must
use the linear search algorithm given below
Worst case:
Clearly the worst case occurs when ITEM is the last element in the array
DATA or is not there at all. In either situation we have
C(n) = n
Average case:
Here we assume that ITEM does appear in DATA, and that is equally likely
to occur at any position in the array. Accordingly the number of comparisons
can be any of the number 1,2,3……….n and each number occurs with the
probability p = 1/n. Then
= ( 1 + 2 + 3 ……………….+ n) .(1/n)
= n.(n+1)/2.(1/n)
= (n+1)/2
Binary Search
DATA[BEG],DATA[BEG+1],DATA[BEG+2]….DATA[END]
The variables BEG and END denote respectively the beginning and end
locations of the segment under consideration. The algorithm compares
ITEM with the middle element DATA [MID] of the segment, where MID is
obtained by
a) If ITEM < DATA[MID] , then ITEM can appear only in the left half
of the segment:
DATA[BEG],DATA[BEG+1]……………..DATA[MID-1]
b) If ITEM > DATA[MID] , then ITEM can appear only in the right half
of the segment:
DATA[MID+1],DATA[MID+2] ……………..DATA[END]
Example
DATA: 11 22 30 33 40 44 55 60 66 77 80 88 99
(a) Suppose ITEM = 40. The search for ITEM in the array DATA is
pictured where the values of the DATA[BEG] and DATA[END] in
each stage of the algorithm is indicated by circles and the value of
DATA[MID] by a square.
(2) Since 40 < 55 , END has its value changed END = MID-1=6
HENCE MID = INT((1+6)/2) = 3 so DATA{MID] = 30
(2) Since 85>55 BEG has its value changed by BEG = MID + 1= 8.
Hence
(3) Since 85 > 77 , BEG has its value changed by BEG = MID+1 = 11
Hence
(4) Since 85 < 88, END has its value changed by END = MID – 1=11.
Hence
Since 85 >80, BEG has its value changed BEG = MID + 1 = 12 . But
now BEG > END. Hence ITEM does not belong to DATA.
BINARY( DATA, LB, UB, ITEM, LOC)
Here DATA is a sorted array with lower bound Lb and upper bound UB
and ITEM is a given item of information. The variables BEG, END and
MID denote respectively the beginning, end and middle locations of a
segment of elements of DATA. This algorithm finds the location LOC of
ITEM in DATA or sets LOC : = NULL
In Binary search each comparison reduces the sample size in half. Hence
we require at most f(n) comparisons to locate ITEM where
Ideally, the choice between Linear and Binary Search should be taken
based on the number of search operations that are likely to be performed
over the List. As the number increases Binary search becomes more
efficient than linear search.