0% found this document useful (0 votes)
0 views11 pages

Linear Search vs Binary Search

The document compares Linear Search and Binary Search algorithms, outlining their methodologies, time complexities, and use cases. Linear Search is simple and works on unsorted data with a time complexity of O(n), while Binary Search is more efficient, requiring sorted data and achieving a time complexity of O(log n). Code examples in multiple programming languages illustrate both search methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views11 pages

Linear Search vs Binary Search

The document compares Linear Search and Binary Search algorithms, outlining their methodologies, time complexities, and use cases. Linear Search is simple and works on unsorted data with a time complexity of O(n), while Binary Search is more efficient, requiring sorted data and achieving a time complexity of O(log n). Code examples in multiple programming languages illustrate both search methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Linear Search vs Binary Search

Last Updated : 19 Dec, 2023



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.

Below is the code syntax for the linear search.


 C++
 C
 Java
 Python
 C#
 Javascript

// Linear Search in C++

#include <iostream>
using namespace std;

int search(int array[], int n, int x)

// Going through array sequencially

for (int i = 0; i < n; i++)

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

Below is the code syntax for the binary search.


 C++
 C
 Java
 Python
 C#
 Javascript
#include <iostream>

using namespace std;

int binarySearch(int array[], int x, int low, int high)

// Repeat until the pointers low and high meet each

// other

while (low <= high) {

int mid = low + (high - low) / 2;

if (array[mid] == x)

return mid;

if (array[mid] < x)

low = mid + 1;

else
high = mid - 1;

return -1;

Important Differences

Linear Search Binary Search

In linear search input data need not to be in In binary search input data need to be in
sorted. sorted order.

It is also called sequential search. It is also called half-interval search.

The time complexity of binary


The time complexity of linear search O(n).
search O(log n).

Multidimensional array can be used. Only single dimensional array is used.

Linear search performs equality Binary search performs ordering


comparisons comparisons

It is less complex. It is more complex.

It is very slow process. It is very fast process.

Let us look at an example to compare the two:


Linear Search to find the element “J” in a given sorted list from A-X
Binary Search to find the element “J” in a given sorted list from A-X

LINEAR SEARCHING EXAMPLE:


 C++
 C
 Java
 Python
 C#
 Javascript
#include <iostream>

using namespace std;

int search(int array[], int n, int x)

// Going through array sequencially

for (int i = 0; i < n; i++)

if (array[i] == x)

return i;

return -1;

int main()

int array[] = { 12, 114, 0, 4, 9 };

int x = 4;

int n = sizeof(array) / sizeof(array[0]);


int result = search(array, n, x);

(result == -1)

? cout << "Element not found"

: cout << "Element found at index: " << result;

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>

using namespace std;

int binarySearch(vector<int> arr,int x,int low,int high){

while(low <= high){

int mid = low + (high - low)/2;

if (arr[mid] == x)

return mid;

else if (arr[mid] < x)

low = mid + 1;

else

high = mid - 1;

}
return -1;

int main(){

vector<int> arr = {2, 4, 5, 7, 14, 17, 19, 22};

int x = 22;

int result = binarySearch(arr, x, 0, arr.size()-1);

if (result != -1)

cout << result << endl;

else

cout << "Not found" << endl;

return 0;

// The code is constributed by Nidhi goel

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

Most Common Searching Algorithms


Other Searching Algorithms
Comparisons between Searching Algorithms
o
Linear Search vs Binary Search
Prerequisite: Linear SearchBinary SearchLINEAR 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
11 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

Library implementations of Searching algorithms


Easy problems on Searching algorithms
Medium problems on Searching algorithms
Hard problems on Searching algorithms

You might also like