L13a_Basic_Searching Techniques
L13a_Basic_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 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
2
Linear Search
Sequential Search with Arrays
3
Flowchart: Sequential Search with Array
Start
i=0
Yes No
K = A[i]?
i = i+1
No
Print "Successful" i≥n
Yes
Print "Unsuccessful"
Stop
4
Example: Sequential Search with Array
int main()
{
int A[10], i, n, K, flag = 0;
printf("Enter the size of an array: ");
scanf("%d",&n);
5
Complexity Analysis
n 1
1 p1 p 2 pi p n
T ( n)
n
i
i 1
n
n 1
T ( n)
2
6
Complexity Analysis : Summary
7
Binary Search
l m id = (l+ u )/2 u
l u = m id -1 u
S erach th is h alf th e sam e w ay
if K < A [m id ]
m id
l l = m id + 1 u
S erach th is h alf th e sam e w ay
if K > A [m id ]
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
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;
10
Binary Search (with Iteration)
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;
}
11
Binary Search (with Recursion)
#include<stdio.h>
int main(){
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;
}
12
Binary Search (with Recursion)
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);
}
13
Complexity Analysis
5
< = >
2 8
< = > < = >
1 3 6 9
< = > < = > < = > < = >
F F F 4 F 7 F 10
< = > < = > < = >
F F F F F F
Complexity Analysis: Binary Search
5
< = >
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 k 1
2 n 2 k 1 , then the binary search algorithm requires either k-1 or k number of comparisons.
Complexity Analysis: Binary Search
• Best case
T(n) = 1
• Worst case
log=2 n 1
T(n)
Interpolation Search
• The Interpolation Search is an improvement over Binary Search
for instances, where the values in a sorted array are uniformly
distributed.
• For example, if the value of the key is closer to the last element,
interpolation search is likely to start search toward the end side.
Interpolation Search
1. l = 1, u = n // Initialization: Range of searching
2. flag = FALSE // Hold the status of searching
3. While (flag = FALSE) do
K A[l ]
4. loc u l l
A[u ] A [l ]
5. If ( l loc u ) then // If loc is within the range of the list
6. Case: K < A[loc]
7. u = loc -1
8. Case: K = A[loc]
9. flag = TRUE
10. Case: K > A[loc]
11. l = loc +1
12. Else
13. Exit()
14. EndIf
15. EndWhile
16. If (flag) then
17. Print “Successful at” loc
18. Else
19. Print “Unsuccessful”
20. EndIf
21. Stop
Complexity Analysis:
Interpolation Search
Interpolation search
Unsuccessful n
n n
Sequential Search with Linked List
Sequential Search with Linked List
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
Example: Sequential Search with LL
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *header = NULL;
int K, n;
return 0;
}
23
Example: Linear Search with LL
void generate(struct node *head, int n)
{
int i;
struct node *temp;
Number of key
Case Asymptotic complexity Remark
comparisons
Case 2
n 1 T(n) = O(n) Average case
T ( n)
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).
Problems to ponder…
6. Out of sequential search and binary search, which is faster? Why?
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?
If you try to solve problems yourself, then you will learn
many things automatically.