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

DSA Lab 4

The document is a laboratory manual for a Data Structures and Algorithms course, focusing on the implementation of Binary Search. It outlines the lab objectives, tools required, theoretical background, and provides a detailed algorithm and pseudo-code for performing a binary search in C++. Additionally, it includes a rubric for evaluating student performance and observations from the lab experience.
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)
8 views

DSA Lab 4

The document is a laboratory manual for a Data Structures and Algorithms course, focusing on the implementation of Binary Search. It outlines the lab objectives, tools required, theoretical background, and provides a detailed algorithm and pseudo-code for performing a binary search in C++. Additionally, it includes a rubric for evaluating student performance and observations from the lab experience.
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/ 6

LABORATORY MANUAL

Electrical Engineering Department

Data Structures and Algorithms

Submitted by

Huzaifa Ali 59384


Instructor
Mr. Muhammad Atif
Assistant professor, Department of Computer Science,
Faculty of Information & Communication Technology, BUITEMS, Quetta.

Lab Engineer
Engr. Ali Israr
Lab Engineer, Department of Electrical Engineering,
Faculty of Information & Communication Technology, BUITEMS, Quetta.
Session Fall – 2024
LAB MANUAL Data Structures and Algorithms

LAB NO. 4 8/ 10 / 2024

Implementation of Binary Search.


Lab outcomes:
After completing this lab, students will be able to.

Implement binary search using function.


Corresponding CLO and PLO:
CLO-1, PLO-5 (Modern Tool Usage)

Tools:
VS Code
Dev C++

Theory:
Before searching, the list of items should be sorted in ascending order. First compare the
key value with the item in the mid position of the array. If there is a match, we can return
immediately the position. if the value is less than the element in middle location of the array,
the required value is lie in the lower half of the array. If the value is greater than the
element in middle location of the array, the required value is lie in the upper half of the
array. We repeat the above procedure on the lower half or upper half of the array.
Procedure:
Algorithm:
Binary_Search (A [ ], U_bound, VAL)
Step 1: set BEG = 0 , END = U_bound , POS = -1 Step 2 : Repeat while (BEG <= END )
Step 3: set MID = ( BEG + END ) / 2
Step 4: if A [ MID ] == VAL then POS = MID
print VAL “ is available at “, POS GoTo Step 6
End if
if A [ MID ] > VAL then set END = MID – 1
Else
set BEG = MID + 1
End if End while
Step 5: if POS = -1 then
print VAL “ is not present “ End if
Step 6: EXIT
Pseudo-code:
Procedure binary_search
LAB MANUAL Data Structures and Algorithms

A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.

set midPoint = lowerBound + ( upperBound - lowerBound ) / 2

if A[midPoint] < x
set lowerBound = midPoint + 1

if A[midPoint] > x
set upperBound = midPoint - 1

if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
Illustration:
LAB MANUAL Data Structures and Algorithms

Lab Experiments/Tasks:
Task-1: Binary Search
Suppose a one-dimensional array AR containing integers is arranged in ascending order. Write a
user-defined function in C++ to search for an integer from AR with the help of Binary search method,
returning an integer 0 to show absence of the number and integer 1 to show presence of the number in
the array. Function should have three parameters: (i) array AR (ii) the number to be searched and (iii)
the number of elements N in the array.
Code
//Huzaifa Ali 59384
#include <iostream>
using namespace std;
int Binarysearch(int arr[],int n , int key){
int s=0;
int e=n;
while(s<=e){
int mid=(s+e)/2;
if(arr[mid]==key){
return mid;
}
else if(arr[mid]>key){
e=mid-1;
}
else{
s=mid+1;
}

}
return -1;
}
LAB MANUAL Data Structures and Algorithms

int main() {
int n;
cout<<"Enter integers";
cin>>n;
int arr[n];
for(int i=0 ; i<n ; i++){
cin>>arr[i];
}
int key ;
cout<<"Enter The Key";
cin>>key;
cout<<Binarysearch(arr , n , key)<<endl;
return 0;
}

Output

Observations:
In this lab, we gained knowledge about executing a Binary search with the assistance of a function,
and we also acquired insights into performing a binary search using a function within the Dev C++
environment.
LAB MANUAL Data Structures and Algorithms

Rubrics:
Absent Student is Student can Student has Student has Student
unable to understand followed constructed perfectly
follow the the provided instructions the implemented a
provided laboratory to construct functional/ working
instructions instructions the working model/ logic/
properly. and familiar fundamental schematic/ circuit/ block
The student with the lab schematic/ model/ block diagram/ code
can name the environment block diagram/ and
Demonstration hardware or (Trainer/ diagram/ code, and successfully
simulation software/ code/ model have executed the
platform, but IDE), but on the successfully lab objective in
unable to cannot protoboard/ executed the Realtime or in
implement implement on trainer/ program/ run a simulation
anything the platform simulation circuit on environment
practically or practically or software. software and produced
on the software on the platform the desired
software results
Category Ungraded Very Poor Poor Fair Good Excellent
Percentage [0] [1-20] [21-40] [41-60] [61-80] [81-100]
Marks 0.0 0.1 0.2 0.3 0.4 0.5
Date Total Marks Instructor’s Signature

Report not Plagiarized Requirements Observations Appropriate Correctly


submitted content are listed and are recorded computations drawn
presented or experimental along with or numerical conclusion
Laboratory incomplete procedure is detailed analysis is with
Reports submission presented procedure performed exact results
and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Percentage [0] [1-20] [21-40] [41-60] [61-80] [81-100]
Marks 0.0 0.1 0.2 0.3 0.4 0.5
Date Total Marks Instructor’s Signature

You might also like