Documente Academic
Documente Profesional
Documente Cultură
Debasis Samanta
Computer Science & Engineering
Indian Institute of Technology Kharagpur
Spring-2017
Lecture #11
Searching Techniques
* Searching Techniques
B tree searching
B+ tree searching
Digital search
B-tree search
Graph search
i=0
Yes No
K = A[i]?
i = i+1
No
Print "Successful" i≥n
Yes
Print "Unsuccessful"
Stop
1
1 n
T ( n) i p1 p2 pi pn
n i 1 n
n 1
T ( n)
2
CS 11001 : Programming and Data Structures 10 Lecture #11: © DSamanta
Complexity Analysis : Summary
n 1
Case 3 T ( n) T(n) = O(n) Average case
2
mid
mid
l l u mid = l(l+u)/2
= mid-1 = mid+1 uu
Serach this half the same way Serach this half the same way
if K < A[mid] if K > A[mid]
(c) Search
(a)
(b)An the
Searchentire
ordered list turns
the array
entire into the
oflistelemnets
turns searching
with
into theindex of right-half
values
searching u andonly
of l,left-half
mid
only
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 <stdio.h>
int main()
{
int i, l, u, mid, n, K, data[100];
l = 0;
u = n - 1;
Contd…
mid = (l+u)/2;
while (l <= u) {
if (data[mid] < K)
l = mid + 1;
else if (data[mid] == K) {
printf("%d found at location %d.\n", search, mid+1);
break;
}
else
u = mid - 1;
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:-
• 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 n 2 k 1, then the binary search algorithm requires k comparisons.
• 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
n
Unsuccessful n n
100
80 Search array
Time ( s)
60
Binary search
40
Interpolation search
20
0 Search list
10 100 500 1000 5000 10000
Input Size
100
80 Search Array
Time ( s)
60
Binary Search
40
Interpolation search
20
0 Search list
10 100 500 1000 5000 10000
Input Size
DATA LINK
Header
Search at an intermediate node:
Search begins Search stops here if key matches Search unsuccessfully ends here
here else move to its immediate next node
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
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.