0% found this document useful (0 votes)
147 views2 pages

Dsu 2nd Practical

This document describes linear and binary search algorithms for searching an array. It includes functions for linear search (linsearch) and binary search (binsearch). The main function allows the user to choose between linear and binary search and to input an array and search key. It then calls the appropriate search function and prints whether the key was found and its location.

Uploaded by

Chetan Badgujar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views2 pages

Dsu 2nd Practical

This document describes linear and binary search algorithms for searching an array. It includes functions for linear search (linsearch) and binary search (binsearch). The main function allows the user to choose between linear and binary search and to input an array and search key. It then calls the appropriate search function and prints whether the key was found and its location.

Uploaded by

Chetan Badgujar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

Linsearch And Binsearch Operation of An Array

#include<stdio.h>
#include<conio.h>
int binsearch (int a[], int i, int j, int key);
int linsearch (int a[], int n, int key);
int main()
{
int a[30],key,n,i,result,op;
clrscr();
do
{
printf("\n1 Linear Search:");
printf("\n2 Binary Search\n3 Quit:");
printf("\n Enter Your Choice:");
scanf("%d",&op);
switch(op)
{
case1: printf("\n Enter no of elements:");
scanf("%",&n);
printf("\n Enter a list of %d elements:",n);

for(i=0;i<=n;i++)
scanf("%d",&a[i]);
printf("\n Enter the element to be searched:");
scanf("%d",&key);

result=linsearch (a,n,key);
if(result ==-1)
printf("\n Not Found:");
else
printf("\n Found at location=%d",result+1);
break;

case2: printf("\n Enter no. of elements:");


scanf("%d",&n);
printf("\n Enter a sorted list of %d elements:",n);
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
printf("\n Enter the element to be searched:");
scanf("%d",&key);
result=binsearch(a,0,n-1,key);
if(result ==-1)
printf("\n Not Found:");
else
printf("\n Found at location=%d",result+1);
break;
}
}while(op!=3);
return(0);
}
int binsearch (int a[],int i, int j, int key)
{
int c;
if(i>j)
return (-1);
c=(i+j)/2;
if(key==a[c])
return (c);
if(key>a[c])
return (binsearch (a, c+1, i,key));
return (binsearch (a,i,c-1,key));
}
int (linsearch (int a[], int n, int key))
{
int i;
for(i=0;i<=n;i++)
{
if(a[i]==key)
return(i);
}
return(-1);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy