Odd Rule Presentation
Odd Rule Presentation
Types of searching
1) Linear search
2) Binary search
Linear search
#include<stdio.h>
main()
{
int a[20],i,n,key,found=-1;
if(found==-1)
printf("Sorry element not found");
else
printf("Element %d is found at %d position",key,found);
}
Binary search
#include<stdio.h>
main()
{
int a[20],i,j,n,key,mid,low,high,temp;
printf("After sorting\n");
for(i=0; i<n; i++)
printf("%d\t",a[i]);
low=0;
high=n-1;
mid=(low+high)/2;
while(low<=high && a[mid]!=key)
{
if(key>a[mid])
low=mid+1;
else
high=mid-1;
mid=(low+high)/2;
}
if(a[mid]==key)
printf("Element %d found at %d position",key,mid+1);
else
printf("Sorry element not found");
}
#include<stdio.h>
main()
{
int a[20],i,n,max,found;
max=a[0];
for(i=1; i<n; i++)
if(a[i]>max)
{
max=a[i];
found=i;
}
#include<stdio.h>
main()
{
int a[10],b[10],c[10],m,n,i,temp;
if(m<n)
temp=m;
else
temp=n;
printf("After adding\n");
for(i=0; i<temp; i++)
printf("%d\t",c[i]);