21. Searching
Linear Search Binary Search
EXTRA
Linear Search- This is the simplest technique to find
out an element in an unsorted list.
In this technique the value of the key is compared with the
first element of the list, if match is found then the search is
terminated. Otherwise next element from the list is fetched
and compared with the key and this process is continued till
the key is found or list is completely.
22. EXTRA
Binary Search- Binary search works for sorted lists and is a
very efficient searching technique. It is used to find the
location of a given element or record in a list. Other
information associated with the element can also be
fetched if required. To find the location of a file in the
computer directory one can use this searching technique.
Let low represents lower limit of the list l, high upper and
mid is average of low and high that is middle of the lower
and upper limits.
Mid = (low+high)/2;
we compare the middle element with key to be searched. If
the value of middle element is greater than key ,then key
will exit in lower half of the list, take high = mid-1 and find
new value of mid.
23. #include<stdio.h>
#include<conio.h>
void main()
{
int arr[100],s, i, first=0, mid=0, last =0, count =0, num;
clrscr();
printf(“Enter Array Sizen”);
scanf(“%d”,&s);
printf(“Enter Array Elementn”);
for(i=0;i<s;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“Array Elementn”);
for(i=0;i<s;i++)
{
printf(“%dn”,arr[i]);
}
EXTRA
24. printf(“Enter the no want to search n”);
scanf(“%d”,&num);
last = s-1;
while((first<=last)&&(count==0))
{
mid=(first+last)/2;
if(arr[i]==num)
{
count=mid;
}
else if(arr[i]<num)
{
first=mid+1;
}
else
{
last=mid-1;
}
}
EXTRA