Module4 Searching Techniques
Module4 Searching Techniques
Debasis Samanta
Computer Science & Engineering
Indian Institute of Technology Kharagpur
Spring-2017
Lecture #11
Searching Techniques
* Searching Techniques
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 se 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 se 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
printf("Enter
printf("Enter thethe elements
elements ofof the
the array:
array: ");
");
for(i=0;
for(i=0; ii << n;
n; i++)
i++)
scanf("%d",&A[i]);
scanf("%d",&A[i]);
printf("Enter
printf("Enter thethe number
number toto be
be searched:
searched: ");
");
scanf("%d",&K);
scanf("%d",&K);
for(i=0;i<n;i++){
for(i=0;i<n;i++){
if(a[i]
if(a[i] ==== K){
K){
flag
flag == 1;
1; break;
break;
}}
}}
if(flag
if(flag ==
== 0)
0)
printf("The
printf("The number
number is
is not
not in
in the
the list");
list");
else
else
printf("The
printf("The number
number is
is found
found at
at index
index %d",i);
%d",i);
return
return 0;
0;
}}
1
1 n
T (n) i p1 p 2 pi p n
n i 1 n
n 1
T ( n)
2
CS 11001 : Programming and Data Structures 10 Lecture #11: © DSamanta
Complexity Analysis : Summary
m imd i d
l l u m= i md i=d -l (1l=+ um ) i/ d2 + 1 S e ra c h th is h a lf th e s a m e w a y
uu
S e ra c h th is h a lf th e s a m e w a y
if K < A [m id ] if K > A [ m id ]
( c ) S( ae( )ba r)Ac Shn e toah rr ecd he rnt eht dier eae rnl ri tasi tyr et ou l fri snet sl te iumnr tnno se ttihsn etwo s iett hha er ic nshedi ane rxgc hvo iafn l gur ei ogs fh l t,l- ehu fatal- fnh dao lnfml oyi dn l y
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
CS 11001 : Programming and Data Structures 14 Lecture #11: © DSamanta
Binary Search (with Iteration)
#include
#include <stdio.h>
<stdio.h>
int
int main()
main()
{{
int
int i,
i, l,
l, u,
u, mid,
mid, n,
n, K,
K, data[100];
data[100];
printf("Enter
printf("Enter number
number of
of elements\n");
elements\n");
scanf("%d",&n);
scanf("%d",&n);
printf("Enter
printf("Enter %d
%d integers
integers in
in sorted
sorted order\n",
order\n", n);
n);
for
for (i
(i == 0;
0; ii << n;
n; i++)
i++)
scanf("%d",&array[i]);
scanf("%d",&array[i]);
printf("Enter
printf("Enter value
value to
to find\n");
find\n");
scanf("%d", &K);
scanf("%d", &K);
ll == 0;
0;
uu == nn -- 1;
1; Contd…
mid
mid == (l+u)/2;
(l+u)/2;
while
while (l
(l <=
<= u)
u) {{
if
if (data[mid]
(data[mid] << K)
K)
ll == mid
mid ++ 1;
1;
else
else if (data[mid] ==
if (data[mid] == K)
K) {{
printf("%d
printf("%d found
found at
at location
location %d.\n",
%d.\n", search,
search, mid+1);
mid+1);
break;
break;
}}
else
else
uu == mid
mid -- 1;
1;
mid
mid == (l
(l ++ u)/2;
u)/2;
}}
if
if (l
(l >> u)
u)
printf("Not
printf("Not found!
found! %d
%d is
is not
not present
present in
in the
the list.\n",
list.\n", K);
K);
return
return 0;
0;
}}
int
int data[100],i,
data[100],i, n,
n, K,
K, flag,
flag, l,
l, u;
u;
printf("Enter
printf("Enter the
the size
size of
of an
an array:
array: ");
");
scanf("%d",&n);
scanf("%d",&n);
printf("Enter
printf("Enter the
the elements
elements of
of the
the array
array in
in sorted
sorted order:
order: "" );
);
for(i=0;i<n;i++)
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&a[i]);
printf("Enter
printf("Enter the
the number
number to
to be
be search:
search: ");
");
scanf("%d",&K);
scanf("%d",&K);
l=0,u=n-1;
l=0,u=n-1;
flag
flag == binarySearch(data,n,K,l,u);
binarySearch(data,n,K,l,u);
if(flag==0)
if(flag==0)
printf("Number
printf("Number is
is not
not found.");
found.");
else
else
printf("Number
printf("Number is
is found.");
found.");
Contd…
return
return 0;
0;
}}
CS 11001 : Programming and Data Structures 17 Lecture #11: © DSamanta
Binary Search (with Recursion)
int
int binary(int
binary(int a[],int
a[],int n,int
n,int K,int
K,int l,int
l,int u){
u){
int
int mid;
mid;
if(l<=u){
if(l<=u){
mid=(l+u)/2;
mid=(l+u)/2;
if(K==a[mid]){
if(K==a[mid]){
return(1);
return(1);
}}
else
else if(m<a[mid]){
if(m<a[mid]){
return
return binarySearch(a,n,K,l,mid-1);
binarySearch(a,n,K,l,mid-1);
}}
else
else
return
return binarySearch(a,n,m,mid+1,u);
binarySearch(a,n,m,mid+1,u);
}}
else
else return(0);
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:-
• If 2 k 1 n 2 k, then the binary search algorithm requires at least one comparison and at most k comparisons.
• For unsuccessful search:-
• If , then the binary search algorithm requires k comparisons.
n 2 k 1
• If 2 k 1 n 2 k 1 , then the binary search algorithm requires either k-1 or k number of comparisons.
• Best case
T(n) = 1
• Worst case
T(n) = log 2 n 1
• 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 e a rc h a rra y
Time ( s)
60
B in a r y s e a r c h
40
I n t e r p o la t io n s e a r c h
20
0 S e a r c h lis t
10 100 500 1000 5000 10000
In p u t S iz e
100
80 S e a rc h A rra y
Time ( s)
60
B in a r y S e a r c h
40
In t e r p o la t io n s e a r c h
20
0 S e a r c h lis t
10 100 500 1000 5000 10000
In p u t S iz e
D A TA L IN K
H eader
S e a rc h a t a n in te rm e d ia te n o d e :
S e a rc h b e g in s S e a rc h s to p s h e re if k e y m a tc h e s S e a rc h u n s u c c e s s fu lly e n d s h e re
h e re e ls e m o v e to its im m e d ia te n e x t n o d e
(b ) L in e a r s e a rc h o n a lin k e d lis t
temp = header
while
Print Unsuccessful
temp != NULL
temp = temp->next
T
N temp->data == Y
key
Print Success
Return temp
struct
struct node
node
{{
int
int data;
data;
struct
struct node
node *next;
*next;
};
};
int
int main()
main()
{{
struct
struct node
node *header
*header == NULL;
NULL;
int
int K,
K, n;
n;
printf("Enter
printf("Enter the
the number
number of
of nodes:
nodes: ");
");
scanf("%d",
scanf("%d", &n);
&n);
printf("\nDisplaying
printf("\nDisplaying the
the list\n");
list\n");
generate(header, num);
generate(header, num);
printf("\nEnter
printf("\nEnter key
key to
to search:
search: ");
");
scanf("%d", &key);
scanf("%d", &key);
searchBinary(header,
searchBinary(header, K);
K);
delete(header);
delete(header);
return
return 0;
0;
}}
for
for (i
(i == 0;
0; ii << num;
num; i++)
i++)
{{
temp
temp == (struct
(struct node
node *)malloc(sizeof(struct
*)malloc(sizeof(struct node));
node));
temp->data = rand()
temp->data = rand() % n; % n;
if
if (*header
(*header == == NULL)
NULL)
{{
*header
*header == temp;
temp;
temp->next
temp->next == NULL;
NULL;
}}
else
else
{{
temp->next
temp->next == header;
header;
header = temp;
header = temp;
}}
printf("%d
printf("%d ", ", temp->data);
temp->data);
}}
}}
void
void delete(struct
delete(struct node
node *header)
*header)
{{
struct
struct node
node *temp;
*temp;
temp = header;
temp = header;
while
while (temp
(temp !=
!= NULL)
NULL)
{{
temp
temp == temp->next;
temp->next;
free(header);
free(header);
header
header == temp;
temp;
}}
}}
Number of key
Case Asymptotic complexity Remark
comparisons
n 1
Case 2 T ( n) T(n) = O(n) Average case
2
2. What will be the outcome, if Binary search technique is applied to an array where the data are not
necessarily in sorted order?
3. Whether the Binary search technique is applicable to a linked list? If so, how?
4. In Binary Search, the mid location is calculated and then either left or right part of the mid location is
searched further, if there is no match at the middle is found. As an alternative to check at middle, the
location at one-third (or two-third) position of the array is chosen. Such a searching can be termed as
Ternary Search. Modify the Binary Search algorithm to Ternary Search algorithm. Which algorithm gives
result faster?
5. If T(n) denotes the number of comparisons required to search a key element in a list of n elements, then a)
express T(n) as recursion formula and b) solve T(n).
7. Whether binary search technique can be applied to search a string in a list od strings stored in an array? If
so, revise the Binary search algorithm accordingly.
suppose, 100 records of type Record are stored in an array say, struct Record students[100];
We have to find the student(s), whose date of birth is given in the form dd/mm/yy. How you can search the
array students?