Professional Documents
Culture Documents
Binary Search
Presented By
Mainak Mondal (30901222102)
Reg. NO : 223091010072
Introduction
➢ Linear Search:
➢ Overview of Search Algorithms: Also known as sequential search, Linear Search is one of
the simplest search algorithms. It involves checking each
In this presentation, we will explore two fundamental element in the dataset one by one until the target
search algorithms: Linear Search and Binary Search. The element is found or the entire dataset has been
process of searching is a crucial operation in computer traversed. Linear Search is typically applicable to unsorted
science and is used extensively to find specific elements data, and its simplicity makes it easy to implement.
within a dataset. Both Linear Search and Binary Search
are used to find a particular target value in a collection of
data, but they employ different strategies and have
distinct characteristics.
➢ Binary Search:
Binary Search is a more advanced search algorithm that is
applicable to sorted datasets. It follows a divide-and-
conquer approach, repeatedly dividing the dataset in half
and eliminating half of the remaining elements at each
step. This makes Binary Search significantly more efficient
than Linear Search for large datasets.
Linear Search
Linear search is a simple search algorithm that checks every element in a list or array until the target value is found or all
elements have been checked. It works by iterating through the list, comparing each element to the target value, and returning
the index of the element if it matches the target value. However, if the target value is not found in the list, the algorithm returns
a "not found" result. Linear search has a time complexity of O(n), where n is the number of elements in the list.
Dataset Requirement Can work with both sorted and unsorted datasets. Requires the dataset to be sorted initially for efficient
searching.
Time Complexity O(n) in the worst-case, average-case, and best-case O(log n) in the worst-case, average-case, and best-case
scenarios. scenarios.
▪ Linear Search: Suitable for small datasets or when the data is unsorted. Quick to implement and
applicable when efficiency is not a primary concern.
▪ Binary Search: Ideal for large datasets, especially when the data is already sorted. Offers significantly
faster search times compared to Linear Search for large datasets.
Code Implementation
➢ Linear Search ➢ Binary Search
int binarySearch(int arr[], int n, int target, int low, int high ) {
if (low <= high) {
int linearSearch(int arr[], int n, int target) { int mid = (low + high)/ 2;
for (int i = 0; i < n; i++) { if (arr[mid] == target) {
if (arr[i] == target) { return mid; // Element found at index mid
return i; // Element found at index i } } else if (arr[mid] < target) {
} return binarySearchRecursive(arr, mid + 1, high, target);
return -1; // Element not found in the array } else {
} return binarySearchRecursive(arr, low, mid - 1, target);
}
}
return -1; // Element not found in the array
}
Real-life Applications