Unit 1
Unit 1
In te re n a l s e a rc h in g E x te rn a l s e a rc h in g
B tre e s e a rc h in g
B + tre e s e a rc h in g
A d re s s c a lc u la tio n s e a rc h
L in e a r s e a rc h N o n -lin e ra s e a rc h
S e q u e n tia l s e a rc h T re e s e a rc h
B in a ry s e a rc h B in a ry s e a rc h tre e
In te rp o la tio n s e a rc h A V L tre e s e a rc h
R e d -b la c k tre e s e a rc h
S p la y tre e s e a rc h
D ig ita l s e a rc h
M u lti-w a y tre e s e a rc h
m -w a y tre e s e a rc h
B -tre e s e a rc h
G ra p h s e a rc h
D e p th firs t s e a rc h
B re a d th firs t s e a rc h
i=0
Yes No
K = A[i]?
i = i+1
No
Print "Successful" i≥n
Yes
Print "Unsuccessful"
Stop
1
p1 p 2 pi p n
n
1
T ( n)
n
i
i 1
n
n 1
T ( n)
2
m id
m id
l l u m= idm id
= -1
l(l+
= um)/2
id + 1 uu
S erach this half the sam e w ay S erach th is h alf th e sam e w ay
if K < A [m id] if K > A [m id ]
(c ) S(a)
e(ba rc
)AShneathrde ered
o rch e nthtireenlist
e arraytiretuolist
frnelem
s tuinrntonsets
thinetowseith
tha ercinsea
hdinexrch
g voin
f grig
alu o fhl,t-h
es uaan
lf dalf
left-h o nmlyoidn ly
mid = (l+u)/2
YES NO
K = A[mid]?
YES NO
K < A[mid]?
Search is successful
u = mid-1 l = mid+1
Stop
NO
(l>u)?
YES
Search is unsuccessful
Start
int main()
{
int i, l, u, mid, n, K, data[100];
l = 0;
u = n - 1; Contd…
mid = (l+u)/2;
mid = (l + u)/2;
}
if (l > u)
printf("Not found! %d is not present in the list.\n", K);
return 0;
}
l=0,u=n-1;
flag = binarySearch(data,n,K,l,u);
if(flag==0)
printf("Number is not found.");
else
printf("Number is found.");
Contd…
return 0;
}
int mid;
if(l<=u){
mid=(l+u)/2;
if(K==a[mid]){
return(1);
}
else if(m<a[mid]){
return binarySearch(a,n,K,l,mid-1);
}
else
return binarySearch(a,n,m,mid+1,u);
}
else return(0);
}
5
< = >
2 8
< = > < = >
1 3 6 9
< = > < = > < = > < = >
F F F 4 F 7 F 10
< = > < = > < = >
F F F F F F
2 8
< = > < = >
1 3 6 9
< = > < = > < = > < = >
F F F 4 F 7 F 10
< = > < = > < = >
F F F F F F
Let n be the total number of elements in the list under search and there exist an integer k such that:-
• For successful search:-
• If2 n 2 , then the binary search algorithm requires at least one comparison and at most k
k 1 k
comparisons.
• For unsuccessful
n 2 k 1 search:-
• If2 k 1 n ,then
2 k the
1 binary search algorithm requires k comparisons.
• If , then the binary search algorithm requires either k-1 or k number of comparisons.
• Best case
T(n) = 1
• Worst case
log =2 n 1
T(n)
• Average Case
• Successful search:-
I
T ( n) 1
n
T (n) log 2 n log 2 n 2 1
n n
• Unsuccessful search:-
E
T ' ( n)
n 1
2
T ' (n) log 2 n
n 1
Interpolation search
Unsuccessful n
n n
100
80 S earch array
Time ( s)
60
B inary search
40
Interpolation search
20
0 S earch list
10 100 500 1000 5000 10000
Input Size
100
80 S earch A rray
Time ( s)
60
B inary S earch
40
Interpolation search
20
0 S earch list
10 100 500 1000 5000 10000
Input Size
DATA L IN K
H ead er
S earch at an in term ed iate n od e:
S earch b egin s S earch stop s h ere if k ey m atch es S earch u n su ccessfu lly en d s h ere
h ere else m ove to its im m ed iate n ext n od e
temp = header
while
Print Unsuccessful
temp != NULL
temp = temp->next
T
N temp->data == Y
key
Print Success
Return temp
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *header = NULL;
int K, n;
return 0;
}
Number of key
Case Asymptotic complexity Remark
comparisons
n 1
Case 2 T ( n) T(n) = O(n) Average case
2
33
STEPS FOR SENTINEL LINEAR SEARCH ALGORITHM: