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

Data Structures and Algorithims: Lab Report

This document is a lab report on implementing and analyzing the binary search algorithm. It includes: 1) An introduction to binary search and its time complexity of O(log n) 2) Code to implement binary search on an integer array and test cases showing it finds elements 3) A home task to count the number of employees with the same salary from sample data

Uploaded by

AnasAbbasi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Data Structures and Algorithims: Lab Report

This document is a lab report on implementing and analyzing the binary search algorithm. It includes: 1) An introduction to binary search and its time complexity of O(log n) 2) Code to implement binary search on an integer array and test cases showing it finds elements 3) A home task to count the number of employees with the same salary from sample data

Uploaded by

AnasAbbasi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structures and Algorithims

CIS-318
Lab Report no.3
Spring 2023
Obtained Marks
Total Marks

Lab Engineer Signature &


Comments

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

Department of Electrical Engineering


2 Lab 4 : Binary Search

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.1 Title: Binary Search


3.2 Objectives:
• Understanding the significance of searching algorithm.
• Using Binary Search for finding an element's position in a sorted array..

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:

Sort the array in ascending order.


Find the middle element.
If the middle element is less than the search element, then search in the right half of the array.
If the middle element is greater than the search element, then search in the left half of the array.
If the middle element is equal to the search element, then return the index of the middle element1

Figure 1 Diagram shows steps taken for Binary Search Algoritihim

3.4 Results and Code


Task 1 : Create a function for binarysearch that takes array, size of an array and target value and and
then uses binary search algorithm and return index at which target value is found.

1. Code:
#include <iostream>
using namespace std;
4 Lab 4 : Binary Search

int binarySearch(int arr[], int l, int r, int x)


{
if (r >= l) {
int mid = l + (r - l) / 2;

// If the element is present at the middle


// itself
if (arr[mid] == x)
{
cout << "yes it is present" << endl;
return mid;
}
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);

// Else the element can only be present


// in right subarray
return binarySearch(arr, mid + 1, r, x);
}

// We reach here when element is not


// present in array
cout << "No it is not present" << endl;
return -1;
}

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;

int result = binarySearch(salary, 0, 4, x);

2. Result:

Figure 2Output for Binary search Algorithm when element is present.


5 Lab 4 : Binary Search

Figure 3Output for Binary search Algorithm when element is not present.

3.5 Home tasks


Q1: Write the time-complexity of your code
The time complexity of binary search is O(log n). This means that the number of operations
required to find an element in a sorted array of size n using binary search grows
logarithmically with the size of the array. This makes binary search a very efficient algorithm
for searching large datasets.

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]);

for (int i = 0;i < n;i++)


{
bool duplicate = false;
for (int j = i + 1;j < n;j++)
if (salary[i] == salary[j])
{
duplicate = true;
break;
}
if (duplicate)
{
cout << "Employees with salary " << salary[i] << ": ";
for (int j = 0;j < n;j++)
if (salary[i] == salary[j])
cout << name[j] << " ";
cout << endl;
}
}

Figure 4 Output for employees with similar salaries


6 Lab 4 : Binary Search

3.6 Discussion and Conclusion:


In this lab, we implemented the binary search algorithm and used it to find an element’s position in a sorted
array. The binary search algorithm works by repeatedly dividing the search interval in half until the target
value is found or it is determined that the value is not present in the array. Our implementation of binary
search was successful in finding the index of a target value in a sorted array. The time complexity of our
implementation was O(log n), which means that it can efficiently search large datasets.

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

You might also like