Professional Documents
Culture Documents
2
NAME- PRAVESH AZAN UID- 19BCS2979
AIM: WRITE A PROGRAM TO DEMONSTRATE THE USE OF LINEAR AND BINARY SEARCH TO FIND A GIVEN
ELEMENT IN AN ARRAY.
ALGORITHM –
Binary Search
Linear Search
Start from the leftmost elements of arr[] and one by one compare x with each element of arr[]
PROGRAM CODE:
#include <iostream>
int i;
return i;
return -1;
while (l<=r) {
if (arr[m] == x)
return m;
if(arr[m]<x)
l = m + 1;
else
r = m - 1;
return -1;
int main(void)
{ int n;
cin>>n;
int arr[n];
for(int i =0;i<n;i++)
cin>>arr[i];
int key;
cout<<"enter the key you want to search in array:\n";
cin>>key;
(resultb == -1)
: cout << "Element is present at index " << resultb << " and at position "<<resultb+1<<endl;
(results == -1)
: cout << "Element is present at index " << results << " and at position "<<results+1<<endl;
return 0;
PRORGAM’S EXPLANATION:
The program will ask to search a key element in array with two approaches binary and linear search.
OUTPUT:
LEARNING OUTCOMES (What I have learnt):
EVALUATION COLUMN:
Sr. No. Parameters Marks Obtained Maximum Marks
1. Student Performance (Conduct of experiment) 12
Objectives/Outcomes.
2. Viva Voce 10
3. Submission of Work Sheet (Record) 8
Total 30