Linear and Binary Search
Linear and Binary Search
We look through the array in an orderly fashion, comparing each element with the element we would
like to find, namely the query element.
Whenever we find a matching element, we report the index of the matched element
If we have traversed the entire array and not found a matching element, we report that the element is
not present in the array.
Important Observations
We do not require any ordering of the elements of the array to find a particular element in the array.
This is because we iterate through the entire array for searching the element, so we are guaranteed to
find the element if it present in the array.
If the element is not present in the array we are guaranteed that the algorithm returns failure.
Step by Step Process for Searching an Element
Let's have a final look at the consolidated algorithm to search for an element in an array of N elements:
STEP 1: Start from the leftmost element of list and one by one compare the query element with each
element of the list.
STEP 2: If the query element matches with an element, return the index.
STEP 3: If the query element doesn’t match with any of elements, return failure.
The idea of binary search is to use the information that the array is sorted and reduce the time complexity to
O(Log n). In Binary search, we adopt the following strategy.
Important Observations
It is crucial for the element array to be sorted to be able to search for a query element.
We jump intervals and prune out the array intervals that we are sure will not contain the query element.
element
If the element is not present
ent in the array we are guaranteed that the algorithm returns failure.
Step by Step Process for Binary Searching an Element
Let's have a final look at the consolidated algorithm to search for an element in an array of N elements:
A linear search scans one item at a time, without jumping to any item.
A binary search cuts down your search to half as soon as you find middle of a sorted list.
The middle element is looked to check if it is greater than or less than the value to be searched.
Accordingly, search is done to either half of the given list.
The worst case time complexity is O(logn).
No auxiliary space is required in linear search. The elements of the array are directly compared with the query
element to find a match. Thus the space complexity for the same is O(1).
You need to maintain a start, end and middle variable to know the window of elements you are checking. Thus
constant space is required to perform iterative binary search. Thus the space complexity for the same is O(1).
Linear Search
Linear search is used on collections of items. It relies on the technique of traversing a list from start to end by
exploring properties of all the elements that are found on the way.
For example, consider an array of integers of size N. You should find and print the position of all the elements
with value x. Here, the linear search is based on the idea of matching each element from the beginning of the
list to the end of the list with the integer x, and then printing the position of the element if the condition is `True'.
Implementation:
Binary Search
Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].
A simple approach is to do search. The time complexity of above algorithm is O(n). Another approach to
perform the same task is using Binary Search.
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow
the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or
the interval is empty.
Binary search is the most popular Search algorithm. It is efficient and also one of the most commonly used
techniques that is used to solve problems.
If all the names in the world are written down together in order and you want to search for the position of a
specific name, binary search will accomplish this in a maximum of 35 iterations.
Binary search works only on a sorted set of elements. To use binary search on a collection, the collection must
first be sorted.
When binary search is used to perform operations on a sorted set, the number of iterations can always be
reduced on the basis of the value that is being searched.
The idea of binary search is to use the information that the array is sorted and reduce the time complexity to
O(Log n).
We basically ignore half of the elements just after one comparison.
1. Compare x with the middle element.
2. If x matches with middle element, we return the mid index.
3. Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element.
So we recur for right half.
4. Else (x is smaller) recur for the left half.
Recursive implementation of Binary Search
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
Output :
Element is present at index 3
Iterative implementation of Binary Search
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
Output :
Element is present at index 3