DEPARTMENT OF COMPUTER ENGINEERING
Subject: - DSU Subject Code: 313301
Semester: - III Course: DATA STRUCTURE USING C
Laboratory No: V120 Name of Subject Teacher: Vijayalakshmi
Ma’am
Name of Student: - Ashlesh Kadam Roll Id: - 23203A0008
Experiment No: 4
Title of Experiment *Write a ‘C’ program to Search a particular data from the given Array of
numbers using Binary Search Method.
BINARY SEARCH ( on numbers ):
Algorithm:
STEP 1: Start
STEP 2: Accept n values from user. i.e array element.
STEP 3: Accept element to be searched from user. i.e Search
STEP 4: Set start=0 ,end =n-1
STEP 5: Calculate Middle = start + end /2
STEP 6 : While (start ≤ end) is true go to step 7 else go to step 13.
STEP 7: if (array[middle] == search) is true then go to step 8 else go to step 9
STEP 8: Print location of search element
STEP 9: if (array[middle] < search) is true then go to step 10 else go to step 11
STEP 10: start = middle + 1 and go to step 12.
STEP 11: Calculate end = middle – 1 and go to step 12 was not found.
STEP 12: Calculate Middle = start + end /2 go to step 6
STEP 13: Check if (start > end) is true then go to step 14
STEP 14: Print Element not found
STEP 15: Stop
Page | 1
Code :
Page | 2
Output: -
Page | 3
Practical Related Questions
1. Write a program to find the first and last occurrence of the element 3 in an array of 20 integers
using binary search.
Page | 4
Page | 5
OUTPUT:
2. Given an array of 15 integers, write a program to find the two middle elements using binary
Search.
Page | 6
OUTPUT:-
Exercise
1. State the applications of Binary Search
Page | 7
1. Searching in Sorted Arrays: Finds an element's position efficiently in a sorted array.
2. Binary Search Tree Operations: Manages insertion, deletion, and lookup in BSTs.
3. Sorted Data Structures: Searches elements quickly in balanced trees and sorted lists.
4. Determining Boundaries: Finds the first/last occurrence or smallest/largest value in sorted data.
5. Optimization Problems: Searches for optimal solutions within a range of values.
2. Compare Binary Search with Linear Search
1. Time Complexity:
Binary Search: O(log n) – Efficient for large datasets due to logarithmic time complexity.
Linear Search: O(n) – Less efficient for large datasets as it checks each element sequentially.
2. Data Requirements:
Binary Search: Requires the array to be sorted before performing the search.
Linear Search: Can be used on unsorted arrays and sorted arrays without any preprocessing.
3. Algorithmic Approach:
Binary Search: Divides the search interval in half repeatedly, reducing the search space quickly.
Linear Search: Sequentially examines each element in the array until the target is found or the end is
reached.
4. Space Complexity:
Binary Search: O(1) for iterative implementations; O(log n) for recursive implementations due to stack
space.
Linear Search: O(1) – Only requires a constant amount of extra space.
5. Use Case:
Binary Search: Ideal for searching in large, sorted datasets where quick lookups are needed.
Linear Search: Suitable for small or unsorted datasets, or when simplicity is preferred over efficiency.
3. Discuss and write the requirements for using binary search on an array of numbers.
To successfully use binary search on an array:
1. Ensure the array is sorted.
2. Use an array or another data structure that supports random access.
Page | 8
3. Confirm the array is not empty.
4. Manage search bounds with appropriate initialization and updates.
5. Have a well-defined target element for comparison.
By adhering to these requirements, you can efficiently and correctly apply binary search to find
elements in a sorted array.
Page | 9