SEARCHING
• SEARCHING REFERS TO THE
OPERATION OF FINDING THE
LOCATION OF A GIVEN SEARCH
ITEM IN THE GIVEN COLLECTION
OF ITEMS.
Linear Search or Sequential search
THE MOST STRAIGHT FORWARD METHOD OF FINDING A PARTICULAR ELEMENT IN AN
UNORDERED LIST IS LINEAR SEARCH. THIS TECHNIQUE SIMPLY INVOLVES THE COMPARISON
OF EACH ELEMENT OF THE LIST IN A SEQUENTIAL MANNER UNTIL THE DESIRED ELEMENT IS
FOUND.
ALGORITHM: LINEAR SEARCH
A IS AN ARRAY WITH N ELEMENTS AND SEARCHITEM IS THE ELEMENT TO BE SEARCHED FOR.
STEP1:LOC=-1
STEP 2:FOR I=0 TO N-1 DO
STEP 3:IF(SEARCHITEM = = A[I])THEN
LOC=I
GOTO STEP 6
ENDIF
END OF FOR LOOP
STEP 6: IF(LOC>=0)THEN
DISPLAY ”SEARCHITEM FOUND AT POSITION (LOC+1)”
ELSE
DISPLAY “SEARCHITEM NOT FOUND”
ENDIF
STEP 7:EXIT
Example:
CASE I: ELEMENT FOUND IN THE LIST
CONSIDER THE ELEMENTS 22 11 66 44 WHERE N=4 AND THE SEARCHITEM=66
I=0 ,LOC = -1
A[I]=22
22 != 66 INCREMNT I, AND I=1
A[I]=11
11!= 66 INCREMNT I, AND I=2
A[2] = =66
LOC=2
THEREFORE 66 IS FOUND IN POSITION 3 (LOC+1)
CASE II: ELEMENT NOT FOUND IN THE LIST
CONSIDER THE SEARCH ITEM 99 IN THE ABOVE LIST 22 11 66 44
I=0 ,LOC= -1
A[I]=22
22 != 99 INCREMNT I, AND I=1
A[I]=11
11!= 99 INCREMNT I, AND I=2
A[2] =66
66!=99 INCREMNT I, AND I=3
A[3] =44
/*Program to perform Linear Search using iteration*/
#include<stdio.h>
int linear(int[],int,int);
void main()
{
int i,key,a[20],n,loc=-1;
clrscr();
printf("Enter the number of elementsn");
scanf("%d",&n);
printf("Enter the elementsn");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the search elementn");
scanf("%d",&key);
loc=linear(a,n,key);
if(loc>=0)
printf("Element found at %d positionn",loc+1);
else
printf("Element not foundn");
getch();
}
/*Program to perform Linear Search using iteration*/
#include<stdio.h>
int linear(int[],int,int);
void main()
{
int i,key,a[20],n,loc=-1;
clrscr();
printf("Enter the number of elementsn");
scanf("%d",&n);
printf("Enter the elementsn");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the search elementn");
scanf("%d",&key);
loc=linear(a,n,key);
if(loc>=0)
printf("Element found at %d positionn",loc+1);
else
printf("Element not foundn");
getch();
}
/*Program to perform Linear Search using iteration*/
/*Linear search iterative function*/
int linear(int a[10],int n,int search)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==search)
return(i);
}
return(-1);
}
/*Program to perform Linear Search using iteration*/
/*Linear search iterative function*/
int linear(int a[10],int n,int search)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==search)
return(i);
}
return(-1);
}
/*Program to perform Linear Search using iteration*/
Output:
Enter the number of elements
3
Enter the elements
33 21 19
Enter the search element
21
Element found at 2 position
/*Program to perform Linear Search using recursion*/
#include<stdio.h>
int linear(int[],int,int);
void main()
{
int i,key,a[20],n,loc=-1;
clrscr();
printf("Enter the number of elementsn");
scanf("%d",&n);
printf("enter the elementsn");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the search elementn");
scanf("%d",&key);
loc=linear(a,n,key);
if(loc>=0)
printf("Element found at %d positionn",loc+1);
else
printf("Element not foundn");
getch();
}
/*Linear search recursive function*/
int linear(int a[10],int n,int search)
{
if(n<0)
return(-1);
else
if(a[n-1]==search)
return(n-1);
else
return(linear(a,n-1,search));
}
Complexity of linear search
The complexity of the algorithm is measured by the number of comparisons f(n)
required to find the search element ITEM in the array A consisting of n elements. The worst
case occurs when we search through the entire array A and ITEM does not appear in A.
In this case algorithm requires
f(n)= n+1 comparisons
Thus in the worst case f(n) is proportional to n
The number of comparisons in the average case can be any number 1,2,…. N and
each number occurs with probability P=1/n. then
f (n) = 1.1/n + 2.1/n + ….. n. 1/n
= (1+2+……n). 1/n
= n(n+1)/2. 1/n
= (n+1)/2
Hence f(n) in the average case is approximately equal to half the number of elements in the
list.
In the best case the element can be found in the first comparison itself where f(n)=1.
Binary Search:
An element can be found in an ordered list by using Binary
search. In this the search element is compared with the
middle element in the list. If the search element is lesser than
the middle element the binary search is performed again only
on the elements to the left of the middle element. If the
search element is greater than the middle element then the
search is made to the right of middle element. This process
continues until the desired element is found or the search list
becomes empty.
Algorithm: Binary Search
A is a sorted array with n elements and searchitem is the element to be searched for in the list.
low and high is used to identify the first and last elements in the range and mid gives the
position of middle element.
Step1: Initialize the variables
low=0, high=n-1,LOC=-1
Step2: while(low<= high)do
mid=(low+high)/2
Step 3: if(searchitem = = A[mid])
LOC=mid
GOTO Step 6
Endif
Step 4:if(searchitem<A[mid])then
high=mid-1
Step 5:else
low=mid+1
Endif
End of while loop
Step 6:if(LOC>=0)then
Display ”searchitem found at position (LOC+1)”
else
Display “searchitem not found”
Endif
Step 7:Exit
Example:
Consider the elements in array A
11 22 33 44 where n=4
and search element ITEM is 11
low = 0(lower bound)
high = 3(upper bound)
mid = (0+3)/2 =1
ITEM<A[mid] so go to the lower half segment.
Therefore reset the value of high to mid-1
low=0 high=0
mid=0
ITEM= =A[0] and search element 11 is found at position 1 (mid+1).
Consider the search element 77 in the above list 11 22 33 44
low = 0(lower bound)
high = 3(upper bound)
mid = (0+3)/2 =1
A[mid]=22
ITEM>A[mid] so go to the upper half segment.
Therefore reset the value of low to mid+1
low=2 high=3
mid=2
A[mid]=33
ITEM>A[mid] so go to the upper half segment.
Therefore reset the value of low to mid+1
low=3 high=3
mid=3
a[mid]=44
ITEM>A[mid] so go to the upper half segment.
Therefore reset the value of low to mid+1
low=4
Now low>high and therefore conclude that the element is not found in the list.
/*Program to perform Binary Search using iteration*/
#include<stdio.h>
int binary(int[],int,int,int);
void main()
{
int i,key,a[20],n,loc=-1;
clrscr();
printf("Enter the number of elementsn");
scanf("%d",&n);
printf("enter the elementsn");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the search elementn");
scanf("%d",&key);
loc=binary(a,0,n-1,key);
if(loc>=0)
printf("Element found at %d positionn",loc+1);
else
printf("Element not foundn");
getch(); }
/*Binary search iterative function*/
int binary(int a[10],int low,int high,int item)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(item==a[mid])
return(mid);
else if(item>a[mid])
low=mid+1;
else if(item<a[mid])
high=mid-1;
}
return(-1);
Thank
you!

Linear and binary search

  • 1.
    SEARCHING • SEARCHING REFERSTO THE OPERATION OF FINDING THE LOCATION OF A GIVEN SEARCH ITEM IN THE GIVEN COLLECTION OF ITEMS.
  • 2.
    Linear Search orSequential search THE MOST STRAIGHT FORWARD METHOD OF FINDING A PARTICULAR ELEMENT IN AN UNORDERED LIST IS LINEAR SEARCH. THIS TECHNIQUE SIMPLY INVOLVES THE COMPARISON OF EACH ELEMENT OF THE LIST IN A SEQUENTIAL MANNER UNTIL THE DESIRED ELEMENT IS FOUND. ALGORITHM: LINEAR SEARCH A IS AN ARRAY WITH N ELEMENTS AND SEARCHITEM IS THE ELEMENT TO BE SEARCHED FOR. STEP1:LOC=-1 STEP 2:FOR I=0 TO N-1 DO STEP 3:IF(SEARCHITEM = = A[I])THEN LOC=I GOTO STEP 6 ENDIF END OF FOR LOOP STEP 6: IF(LOC>=0)THEN DISPLAY ”SEARCHITEM FOUND AT POSITION (LOC+1)” ELSE DISPLAY “SEARCHITEM NOT FOUND” ENDIF STEP 7:EXIT
  • 3.
    Example: CASE I: ELEMENTFOUND IN THE LIST CONSIDER THE ELEMENTS 22 11 66 44 WHERE N=4 AND THE SEARCHITEM=66 I=0 ,LOC = -1 A[I]=22 22 != 66 INCREMNT I, AND I=1 A[I]=11 11!= 66 INCREMNT I, AND I=2 A[2] = =66 LOC=2 THEREFORE 66 IS FOUND IN POSITION 3 (LOC+1) CASE II: ELEMENT NOT FOUND IN THE LIST CONSIDER THE SEARCH ITEM 99 IN THE ABOVE LIST 22 11 66 44 I=0 ,LOC= -1 A[I]=22 22 != 99 INCREMNT I, AND I=1 A[I]=11 11!= 99 INCREMNT I, AND I=2 A[2] =66 66!=99 INCREMNT I, AND I=3 A[3] =44
  • 4.
    /*Program to performLinear Search using iteration*/ #include<stdio.h> int linear(int[],int,int); void main() { int i,key,a[20],n,loc=-1; clrscr(); printf("Enter the number of elementsn"); scanf("%d",&n); printf("Enter the elementsn"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the search elementn"); scanf("%d",&key); loc=linear(a,n,key); if(loc>=0) printf("Element found at %d positionn",loc+1); else printf("Element not foundn"); getch(); }
  • 5.
    /*Program to performLinear Search using iteration*/ #include<stdio.h> int linear(int[],int,int); void main() { int i,key,a[20],n,loc=-1; clrscr(); printf("Enter the number of elementsn"); scanf("%d",&n); printf("Enter the elementsn"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the search elementn"); scanf("%d",&key); loc=linear(a,n,key); if(loc>=0) printf("Element found at %d positionn",loc+1); else printf("Element not foundn"); getch(); }
  • 6.
    /*Program to performLinear Search using iteration*/ /*Linear search iterative function*/ int linear(int a[10],int n,int search) { int i; for(i=0;i<n;i++) { if(a[i]==search) return(i); } return(-1); }
  • 7.
    /*Program to performLinear Search using iteration*/ /*Linear search iterative function*/ int linear(int a[10],int n,int search) { int i; for(i=0;i<n;i++) { if(a[i]==search) return(i); } return(-1); }
  • 8.
    /*Program to performLinear Search using iteration*/ Output: Enter the number of elements 3 Enter the elements 33 21 19 Enter the search element 21 Element found at 2 position
  • 9.
    /*Program to performLinear Search using recursion*/ #include<stdio.h> int linear(int[],int,int); void main() { int i,key,a[20],n,loc=-1; clrscr(); printf("Enter the number of elementsn"); scanf("%d",&n); printf("enter the elementsn"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the search elementn"); scanf("%d",&key); loc=linear(a,n,key); if(loc>=0) printf("Element found at %d positionn",loc+1); else printf("Element not foundn"); getch(); }
  • 10.
    /*Linear search recursivefunction*/ int linear(int a[10],int n,int search) { if(n<0) return(-1); else if(a[n-1]==search) return(n-1); else return(linear(a,n-1,search)); }
  • 11.
    Complexity of linearsearch The complexity of the algorithm is measured by the number of comparisons f(n) required to find the search element ITEM in the array A consisting of n elements. The worst case occurs when we search through the entire array A and ITEM does not appear in A. In this case algorithm requires f(n)= n+1 comparisons Thus in the worst case f(n) is proportional to n The number of comparisons in the average case can be any number 1,2,…. N and each number occurs with probability P=1/n. then f (n) = 1.1/n + 2.1/n + ….. n. 1/n = (1+2+……n). 1/n = n(n+1)/2. 1/n = (n+1)/2 Hence f(n) in the average case is approximately equal to half the number of elements in the list. In the best case the element can be found in the first comparison itself where f(n)=1.
  • 12.
    Binary Search: An elementcan be found in an ordered list by using Binary search. In this the search element is compared with the middle element in the list. If the search element is lesser than the middle element the binary search is performed again only on the elements to the left of the middle element. If the search element is greater than the middle element then the search is made to the right of middle element. This process continues until the desired element is found or the search list becomes empty.
  • 13.
    Algorithm: Binary Search Ais a sorted array with n elements and searchitem is the element to be searched for in the list. low and high is used to identify the first and last elements in the range and mid gives the position of middle element. Step1: Initialize the variables low=0, high=n-1,LOC=-1 Step2: while(low<= high)do mid=(low+high)/2 Step 3: if(searchitem = = A[mid]) LOC=mid GOTO Step 6 Endif Step 4:if(searchitem<A[mid])then high=mid-1 Step 5:else low=mid+1 Endif End of while loop Step 6:if(LOC>=0)then Display ”searchitem found at position (LOC+1)” else Display “searchitem not found” Endif Step 7:Exit
  • 14.
    Example: Consider the elementsin array A 11 22 33 44 where n=4 and search element ITEM is 11 low = 0(lower bound) high = 3(upper bound) mid = (0+3)/2 =1 ITEM<A[mid] so go to the lower half segment. Therefore reset the value of high to mid-1 low=0 high=0 mid=0 ITEM= =A[0] and search element 11 is found at position 1 (mid+1). Consider the search element 77 in the above list 11 22 33 44 low = 0(lower bound) high = 3(upper bound) mid = (0+3)/2 =1 A[mid]=22 ITEM>A[mid] so go to the upper half segment. Therefore reset the value of low to mid+1 low=2 high=3 mid=2 A[mid]=33 ITEM>A[mid] so go to the upper half segment. Therefore reset the value of low to mid+1 low=3 high=3 mid=3 a[mid]=44 ITEM>A[mid] so go to the upper half segment. Therefore reset the value of low to mid+1 low=4 Now low>high and therefore conclude that the element is not found in the list.
  • 15.
    /*Program to performBinary Search using iteration*/ #include<stdio.h> int binary(int[],int,int,int); void main() { int i,key,a[20],n,loc=-1; clrscr(); printf("Enter the number of elementsn"); scanf("%d",&n); printf("enter the elementsn"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the search elementn"); scanf("%d",&key); loc=binary(a,0,n-1,key); if(loc>=0) printf("Element found at %d positionn",loc+1); else printf("Element not foundn"); getch(); }
  • 16.
    /*Binary search iterativefunction*/ int binary(int a[10],int low,int high,int item) { int mid; while(low<=high) { mid=(low+high)/2; if(item==a[mid]) return(mid); else if(item>a[mid]) low=mid+1; else if(item<a[mid]) high=mid-1; } return(-1);
  • 17.