Linear Search vs Binary Search
Linear Search vs Binary Search
Prerequisite:
Linear Search
Binary Search
LINEAR SEARCH
Assume that item is in an array in random order and we have to find an item. Then the
only way to search for a target item is, to begin with, the first position and compare it
to the target. If the item is at the same, we will return the position of the current item.
Otherwise, we will move to the next position. If we arrive at the last position of an
array and still can not find the target, we return -1. This is called the Linear search or
Sequential search.
#include <iostream>
using namespace std;
if (array[i] == x)
return i;
return -1;
BINARY SEARCH
In a binary search, however, cut down your search to half as soon as you find the
middle of a sorted list. The middle element is looked at to check if it is greater than or
less than the value to be searched. Accordingly, a search is done to either half of the
given list
// other
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
return -1;
Important Differences
In linear search input data need not to be in In binary search input data need to be in
sorted. sorted order.
if (array[i] == x)
return i;
return -1;
int main()
int x = 4;
(result == -1)
Output
Element found at index: 3
Time Complexity: O(n), where n is the size of the input array. The worst-case
scenario is when the target element is not present in the array, and the function has to
go through the entire array to figure that out.
Auxiliary Space: O(1), the function uses only a constant amount of extra space to
store variables. The amount of extra space used does not depend on the size of the
input array.
BINARY SEARCHING EXAMPLE:
C++
C
Java
Python
C#
Javascript
#include<bits/stdc++.h>
if (arr[mid] == x)
return mid;
low = mid + 1;
else
high = mid - 1;
}
return -1;
int main(){
int x = 22;
if (result != -1)
else
return 0;
Output
7
Time Complexity: O(log n) – Binary search algorithm divides the input array in half
at every step, reducing the search space by half, and hence has a time complexity of
logarithmic order.
Auxiliary Space: O(1) – Binary search algorithm requires only constant space for
storing the low, high, and mid indices, and does not require any additional data
structures, so its auxiliary space complexity is O(1).
Comment
More info
Advertise with us
Next Article
Interpolation search vs Binary search
Similar Reads
Searching Algorithms
Searching algorithms are essential tools in computer science used to locate specific items within a collection of
data. These algorithms are designed to efficiently navigate through data structures to find the desired
information, making them fundamental in various applications such as databases, we
2 min read
o
Interpolation search vs Binary search
Interpolation search works better than Binary Search for a Sorted and Uniformly Distributed array. Binary
Search goes to the middle element to check irrespective of search-key. On the other hand, Interpolation
Search may go to different locations according to search-key. If the value of the search-k
7 min read
o
Why is Binary Search preferred over Ternary Search?
The following is a simple recursive Binary Search function in C++ taken from here. C/C++ Code // CPP
program for the above approach #include <bits/stdc++.h> using namespace std; // A recursive binary search
function. It returns location of x in // given array arr[l..r] is present, otherwise -1
11 min read
o
Is Sentinel Linear Search better than normal Linear Search?
Sentinel Linear search is a type of linear search where the element to be searched is placed in the last position
and then all the indices are checked for the presence of the element without checking for the index out of
bound case. The number of comparisons is reduced in this search as compared to
8 min read