You are on page 1of 9

Linear Search and

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.

Time complexity Analysis


▪ The time complexity of Linear Search is O(n), where "n" is the number of elements in the
dataset.
▪ The algorithm works by iterating through each element in the dataset one by one, starting from
the beginning, until it finds the target element or reaches the end of the dataset.
▪ Linear Search is particularly useful when dealing with small datasets or when the data is
unsorted.
Binary Search
Binary search is a search algorithm that works by repeatedly dividing the search interval in half. It requires that the list or array
be sorted beforehand. The algorithm starts by comparing the middle element of the list to the target value. If the middle
element is equal to the target value, the algorithm returns the index of the middle element. If the target value is less than the
middle element, the search is narrowed to the lower half of the list, and the process is repeated. If the target value is greater
than the middle element, the search is narrowed to the upper half of the list, and the process is repeated. This continues until
the target value is found or until there are no more elements left to search. Binary search has a time complexity of O(log n),
where n is the number of elements in the list.

Time complexity Analysis


▪ The time complexity of Linear Search is O(log n),where "n" is the number of elements in the
dataset.
▪ This logarithmic complexity means that even with a vast dataset, the number of iterations
required to find the target element remains relatively small..
Differences between Linear
and Binary Search
Topic Linear Search Binary Search
Search Strategy Uses a sequential approach by examining each element in Follows a divide-and-conquer strategy, efficiently
the dataset one by one until the target is found or the end eliminating half of the remaining dataset in each step.
of the dataset is reached.

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.

When To Use Each Algorithm

▪ 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

➢ Linear search ➢ Binary search


▪ Find Elements in an Unsorted List: Linear Search is commonly ▪ Searching in Sorted Arrays or Databases: Binary Search is highly
used when dealing with unsorted datasets. For example, efficient when searching in sorted datasets or arrays.
searching for a name in a phone directory or a contact list.
▪ Finding Elements in Games: Binary Search can be used in game
▪ Searching in Linked Lists: In linked list data structures, linear development to efficiently search for specific elements like
search is often used to find a specific element within the list. game objects in sorted arrays.
▪ Checking for Duplicates: Linear Search can be used to identify ▪ Auto-Complete and Suggestion Systems: In applications like
duplicate entries in a dataset or an array. search engines or text editors, Binary Search can be used to
provide auto-complete suggestions quickly.
▪ Data Validation: Linear Search can be employed to validate user
inputs or check for specific conditions in datasets. ▪ Finding Positions or Ranks: Binary Search can be employed to
find the position or rank of an element in a sorted dataset.
Conclusion
In conclusion, linear search and binary search are two fundamental search algorithms that are commonly used in computer science.
Linear search is a simple algorithm that checks each element in a list or array until the target value is found or all elements have been
checked. It is easy to implement and works on unsorted arrays, but its time complexity of O(n) makes it inefficient for large datasets.
Binary search, on the other hand, is a more complex algorithm that requires the list or array to be sorted beforehand. It works by
repeatedly dividing the search interval in half until the target value is found or the search interval is empty. Binary search has a time
complexity of O(log n), which makes it much faster than linear search for large datasets.
In general, if you have a small dataset or an unsorted array, linear search may be a good choice. However, if you have a large dataset or a
sorted array, binary search is likely to be more efficient. Knowing when and how to use these algorithms can help you write more efficient
and effective code.
Thank you

You might also like