0% found this document useful (0 votes)
23 views

DS Ex3

The document describes an experiment implementing linear and binary search algorithms. The program allows the user to choose between the search methods and input a key to search for in a sample array. For linear search, it iterates through the array and checks if the key matches each element, returning the index if found. Binary search recursively halves the search range at each step based on comparing the key to the middle element, until the key is found or the range is exhausted. The output demonstrates examples of both searches finding and not finding keys in test arrays.

Uploaded by

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

DS Ex3

The document describes an experiment implementing linear and binary search algorithms. The program allows the user to choose between the search methods and input a key to search for in a sample array. For linear search, it iterates through the array and checks if the key matches each element, returning the index if found. Binary search recursively halves the search range at each step based on comparing the key to the middle element, until the key is found or the range is exhausted. The output demonstrates examples of both searches finding and not finding keys in test arrays.

Uploaded by

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

Experiment No.

03

Program Source code:

#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
void linear()
{int a[]={1,3,5,7,9};
int item;
int n=5;
int i=0;
int flag=0;

printf("The original array element are:\n");


for(i=0;i<n;i++)
{
printf("a[%d]=%d\n",i,a[i]);

} printf("enter key to be searched");


scanf("%d",&item);
i=0;
while(i<n)
{
if(a[i]==item)
{
flag=1;
break;
}
i=i+1;
}
if(flag==1)
printf("Found element %d at position %d\n",item,i);
else
printf("Element not found");
}
void binary()
{
int flag = 0;
int a[5];
int first,last,mid,key;
int n,i;
printf("Enter the number of element:");
scanf("%d",&n);
first = 0;
last = n-1;
printf("Enter the element into array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key to be searched:");
scanf("%d",&key);
while(first<=last)
{ mid = (first+last)/2;
if(a[mid]==key)
{
flag =1;
break;
}
else if(a[mid]>key) last = mid-1;
else (first = mid+1);
}
if(flag ==1)
{
printf("Element found");
}
else
{
printf("Element not found");
}

}
void main()
{
int ch;
int element;
while(1)
{printf("\n MENU\n Select your operation:\n1.Linear Search\n2.Binary
Search\n3.Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter element to be searched");
scanf("%d",&element);
linear(element);
break;
case 2:
binary(element);
break;

case 3:
exit(0);

default:
printf("INVALD INPUT");
}
}
}

Output:

MENU
Select your operation:
1.Linear Search
2.Binary Search
3.Exit
Enter your choice:1
Enter element to be searched7
The original array element are:
a[0]=1
a[1]=3
a[2]=5
a[3]=7
a[4]=9
enter key to be searched7
Found element 7 at position 3
MENU
Select your operation:
1.Linear Search
2.Binary Search
3.Exit
Enter your choice:1
Enter element to be searched10
The original array element are:
a[0]=1
a[1]=3
a[2]=5
a[3]=7
a[4]=9
enter key to be searched10
Element not found
MENU
Select your operation:
1.Linear Search
2.Binary Search
3.Exit
Enter your choice:2
Enter the number of element:3
Enter the element into array:7
10
11
Enter the key to be searched:11
Element found
MENU
Select your operation:
1.Linear Search
2.Binary Search
3.Exit
Enter your choice:2
Enter the number of element:3
Enter the element into array:7
10
11
Enter the key to be searched:3
Element not found
MENU
Select your operation:
1.Linear Search
2.Binary Search
3.Exit
Enter your choice:3

You might also like