You are on page 1of 12

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


Experiment: 1.2

Student Name : Abhishek Mishra UID: 21BCS4999

Branch: Computer Science & Engineering Section/Group: 703A

Semester: 3 rd Date of Performance: 24/08/2022

Subject Name: Data Structure Subject Code: 21CSH-211

1. Aim of the practical: Write a program to demonstrate the use


of linear Search to find a given element in an arr

#include <iostream>
using namespace std;
int length;
cout<<"Enter Length of the Array : ";
cin>>length;
int arr[length];
for(int i=0;i<length;i++){

<<"Enter Element at index position "<<i<<"


: "; cin>>arr[i];
}
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


cout<<"Your Entered Array is : ";
for (int i = 0; i < length; i++)
{ cout<<arr[i]<<" ";
} cout<<"]"<<endl; int element;
cout<<"Enter Element to Search : ";
cin>>element; for (int i = 0; i <
length; i++)
{ if (arr[i]==element)
{ cout<<"Element found at index position : "<<i<<endl;
cout<<"and at position : "<<i+1; break;
} if
(i==length-1)
{ cout<<"Element not found in the
array...!!!!"; } } return 0;

cout<<" [ ";
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

ScreenShot of Code :
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

ScreenShot of Output :

2. Aim of the practical:

Write a program to demonstrate the use of Binary Search Search to


find a given element in an array.

Code/Program Code :
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

# #include<iostream>

using namespace std;

int BinarySearch(int arr[],int length,int


element){ int start=0,end = length-1;

while (start<=end)
{
int mid = (start+end)/2;
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

if (arr[mid]==element)
{
return mid;
}
else if (arr[mid]>element)
{
end=mid-1;
} else {
start=mid+1;
} }
return -1;
} int main(){
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


int length; cout<<"Enter length of the Array : ";
cin>>length; int arr[length]; for (int i = 0; i <
length; i++)
{ cout<<"Enter Element at index position "<<i<<" : ";
cin>>arr[i];
} cout<<"Your Entered Array is :
"; cout<<" [ "; for (int i =
0; i < length; i++)
{ cout<<arr[i]<<"
";
} cout<<"]"<<endl; int element;
cout<<"Enter Element to Search : ";
cin>>element;

;
int flag = BinarySearch(arr,length,element); if
(flag!=-1)
{ cout<<"Element Found at index position :
"<<flag<<endl; cout<<"and position : "<<flag+1;
}
else{
cout<<"Entered Element is not present in the Array..!!!";
}
return 0;
}
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

ScreenShot of Code :
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

ScreenShot of Output :

Evaluation Grid (To be filled by Faculty):


DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


Sr. Parameters Marks Maximum
No. Obtained Marks
1. Worksheet completion including writing 10
learning objectives/Outcomes.(To
besubmitted at the end of the day)

2. Post Lab Quiz Result. 5

3. Student Engagement in 5
Simulation/Demonstration/Performance
and Controls/Pre-Lab Questions.

Signature of Faculty (with Date): Total


Marks
Obtained:

You might also like