Data Structures and Algorithims: Lab Report
Data Structures and Algorithims: Lab Report
CIS-318
Lab Report no.3
Spring 2023
Obtained Marks
Total Marks
Student Name
1. Anas Zohrab
Section: A (Electronics)
Experiment No: 03 Date of Submission:
March-13-2023
Experiment Title:
Binary Search
Batch: Teacher:
BSEE 2019-23 Dr. Kamran Safdar
Semester Lab Engineer:
8th Mr. Ghaznooq Ahmad
Table of Contents
Binary Search .................................................................................................................................................... 1
3.1 Title: Implementation of quick sort and merge sort................................................................................ 3
3.2 Objectives: .............................................................................................................................................. 3
3.3 Introduction: ............................................................................................................................................ 3
3.4 Results and Code..................................................................................................................................... 3
1. Code:...................................................................................................................................................... 3
2. Result:.................................................................................................................................................... 4
3.5 Home tasks .............................................................................................................................................. 5
3.6 Discussion and Conclusion: .................................................................................................................... 6
3.7 References: .............................................................................................................................................. 6
3 Lab 4 : Binary Search
3.3 Introduction:
Binary Search:
Binary search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in
half. The idea of binary search is to use the information that the array is sorted and reduce the time
complexity to O (Log n)1. The basic steps to perform Binary Search are:
1. Code:
#include <iostream>
using namespace std;
4 Lab 4 : Binary Search
int main()
{
string name[5] = { "anas", "saqib", "waleed", "mbahsir", "fahad" };
int salary[5] = { 1000,2000,3000,4000,5000 };
int x = 0;
cout << "enter the number you wish to search: ";
cin >> x;
2. Result:
Figure 3Output for Binary search Algorithm when element is not present.
Q2: Suppose in a payroll system several employees have similar salaries, write a
program that counts the number of employees having similar salary and display their
names.
string name[] = { "anas", "saqib", "waleed", "mbahsir", "fahad" };
int salary[] = { 1000, 2000, 3000, 2000, 3000 };
int n = sizeof(name) / sizeof(name[0]);
As part of our home tasks, we also analyzed the time complexity of our code and found that it was O(log n).
This confirms that our implementation of binary search is efficient and can handle large datasets.We also
completed a home task where we wrote a program to count the number of employees having similar salaries
in a payroll system and display their names. This task helped us apply our knowledge of algorithms to solve
a practical problem.
In conclusion, this lab helped us understand the significance of searching algorithms and how they can be
used to efficiently find an element’s position in a sorted array. Our implementation of binary search was
successful and demonstrated the efficiency of this algorithm. In future work, we could explore other
searching algorithms and compare their performance to that of binary search.
3.7 References:
• GeeksforGeeks. (n.d.). Binary Search. Retrieved from https://www.geeksforgeeks.org/binary-search/
• Khan Academy. (n.d.). Binary Search. Retrieved from
https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/binary-
search
• Wikipedia. (n.d.). Binary Search Algorithm. Retrieved from
https://en.wikipedia.org/wiki/Binary_search_algorithm