Linear Search vs Binary Search: Difference Between Linear Search & Binary Search

# Linear Search vs Binary Search: Difference Between Linear Search & Binary Search

Last updated:
23rd Jun, 2024
Views
14 Mins
View All

In my journey through data structures, Iāve navigated the nuances of linear search vs binary search in data structure, especially when dealing with arrays and lists stored in contiguous memory spaces. These structures are great for quickly adding new elements, but finding specific ones can be a different story. For unsorted data, I lean towards linear search, which checks each element one by one, offering a simple yet time-consuming O(n) approach. On the flip side, binary search, which I favor for sorted data, slices the search time significantly to O(log n) by halving the search area with each step. However, it demands that the data be pre-sorted. While other algorithms like exponential and jump search exist, choosing between linear and binary search hinges on whether my data is sorted, affecting the searchās efficiency. This decision crucially shapes the time complexity I can expect.

Also read: Free data structures and algorithm course!

## Linear Search

Linear search algorithm, also known as the sequential search algorithm, is considered to be the simplest searching algorithm. The reason behind it is that one can easily traverse the list completely and match each of the elements of the list with the one whose location they want to find. Thus, it is one of the most popular search algorithms for finding an element from an unordered list.

Must read: Learn excel online free!

The complexity of linear search algorithm is divided into three main cases, named best case, worst case and average case. In best cases, the element can be found in the first position and is finished with a single successful comparison. Then what is the worst case complexity of linear algorithm you may ask?

As the name suggests,  in the worst case, the element may be found at the last position of the array or not at all and thus is finished after the ānā number of comparisons. Thus, it is often denoted with O(n). for the first case, the search succeeds in the ānā number of comparisons but in the next case, the search fails after the ānā number of comparisons.

Likewise,  in the average case, the element can be found in the middle of the array.

The space complexity of the linear search is denoted by O(1).

As already discussed, the linear search algorithm compares each element in the array, and hereās the code to do that.

 ```public class UpGrad{   public static int linear_search(int[] arr, int n, int k){     for(int i=0; i

Letās walk through the code.

Weāve declared a linear_search function, which expects an array, integer key as parameters. Now we need to loop over all the elements and compare whether it matches with our search key, so weāve written a for loop which loops over the array, and inside it, thereās an if loop that checks if the number at that position matches with the search key or not. If we find a match weāll return the position. If there is no such element in the array then weāll return -1 at the end of the function.

Note that if we have multiple appearances of the same number, then we are going to return the position of its first occurrence.

Coming to the main method, weāve declared and initialized an integer array. Then we are initializing the key which has to be searched. Here we are hardcoding the array and key, but you can change it to user input. Now that we have got the list of elements and the key to be searched, the linear search method is called and the returned index is noted. Later we are checking the returned value and printing the index if the key exists, else printing key not found.

## Explore our Popular Data Science Courses

 Executive Post Graduate Programme in Data Science from IIITB Professional Certificate Program in Data Science for Business Decision Making Master of Science in Data Science from University of Arizona Advanced Certificate Programme in Data Science from IIITB Professional Certificate Program in Data Science and Business Analytics from University of Maryland Data Science Courses

### How Linear Search works

Imagine you are in a library with shelves of books. You move to a bookshelf and start looking for a book. With no idea about the order, you start at one end and move all the way up to the other end until you find your book. This is linear search. It iterates through each element in a list sequentially, comparing it to the target element, until a match is found, or the list is exhausted. Here is a simple implementation:

```import time
import random
import numpy as np

def linear_search(arr, x):

"""Performs linear search on an array and measures execution time."""
for i in range(len(arr)):

if arr[i] == x:

return i  # Element found, return its index

# Create a list with one million random integers and choose a random element

arr = [random.randint(1, 1000000) for _ in range(1000000)]
x = arr[934213]

start_time = time.time_ns()  # Start time measurement
result = linear_search(arr, x)

if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")

end_time = time.time_ns()  # End time measurement

# Calculate and print time complexity

time_taken = end_time - start_time
print("Time complexity: O(n) ā", time_taken, "nanoseconds")```

Element is present at index 50718
Time complexity: O(n) ā 2325600 nanoseconds

#### Pros and Cons of Linear Search Pros

• Simple to implement: Itās easy to understand and code, making it a readily accessible algorithm.
• No sorting required: The list doesnāt need to be organized in any specific order.
• Flexible data structures: Works with various data structures like arrays, linked lists, etc.

#### Cons

• Slow for large datasets: As the list size grows, so does the search time, making it inefficient for large data volumes.
• No early termination: It checks every element, even if the target is unlikely to be present in the latter half.

## Binary Search

Differing from linear search algorithm, binary search is an algorithm used for finding an elementās position in a sorted array. If the list is not sorted, one first needs to sort the list and then implement the algorithm to identify the position of the element one wishes to know.

There are two ways in which a binary search algorithm can be implemented that are the iterative method and the recursive method.

Similar to linear search, in binary search also, there are three types of time complexities, that are, best case complexity, denoted by O(1), worst case complexity, denoted by O(log n) and average-case complexity, also denoted by O (log n).

The space complexity in binary search is denoted by O(1).

Binary search is more optimized than linear search, but the array must be sorted to apply binary search. And hereās the code to do that.

 ```public class UpGrad{     public static int binary_search(int[] arr, int k){         int l=0,h=arr.length-1,mid=0;         while(l<=h){             mid=l+(h-l)/2;             if(arr[mid]==k)                 return mid+1;             else if(arr[mid]>k)                 h=mid-1;             else                 l=mid+1;         }         return -1;     }     public static void main(String[] args){         int[] arr=new int[]{1,2,3,4,5,6,7,8,9};         int k=8;         int r=binary_search(arr,k);         if(r==-1)             System.out.println("element not found");         else             System.out.println("element found at "+r+" position");     } }```

Letās walk through the code.

## Read our popular Data Science Articles

 Data Science Career Path: A Comprehensive Career Guide Data Science Career Growth: The Future of Work is here Why is Data Science Important? 8 Ways Data Science Brings Value to the Business Relevance of Data Science for Managers The Ultimate Data Science Cheat Sheet Every Data Scientists Should Have Top 6 Reasons Why You Should Become a Data Scientist A Day in the Life of Data Scientist: What do they do? Myth Busted: Data Science doesnāt need Coding Business Intelligence vs Data Science: What are the differences?

Weāve declared a method binary_search which expects a sorted integer array and an integer key as the parameters. We are initializing the variables low, high, mid. Here low, high are pointers where low will be at 0 index and high will be at n index in the beginning. So how does binary search work?

āupGradās Exclusive Data Science Webinar for you ā

The Future of Consumer Data in an Open Data Economy

ā

At first, weāll calculate the mid of low and high. We can calculate the mid as (low+high)/2, but sometimes high could be a large number, and adding low to the high may lead to integer overflow. So calculating mid as low+(high-low)/2 would be an optimal way.

Weāll compare the element at mid with the search key, and weāll return the index if we find a match. Else weāll check if the mid element is greater than the key or smaller than the key. If the mid is greater then we need to check only the first half of the array because all the elements in the second half of the array are greater than the key, so weāll update the high to mid-1.

Similarly if mid is less than key then we need to search in the second half of the array, hence updating the low to mid+1. Remember binary search is based on the decrease and conquer algorithm since we are ignoring one of the halves of the array in each iteration.

Coming back to our code, we have built the main method. Initialized a sorted array and search key, made a call to the binary search, and printed the results.

Now that weāve walked through the algorithms of both linear search and binary search letās compare both algorithms.

### How Binary Search works

Imagine the same bookshelf scenario, but this time you can teleport halfway through the shelf after each comparison. Binary search leverages this concept. It repeatedly divides the sorted list in half, compares the target element with the middle element, and eliminates half the list based on the comparison. This process continues until the target is found or the search space is narrowed down to zero. Now, here is a sample code:

```def binary_search(arr, x):

"""Performs binary search on a sorted array and measures execution time."""

low = 0
high = len(arr) - 1
mid = 0

while low <= high:
mid = (low + high) // 2

if arr[mid] == x:
return mid  # Element found, return its index

elif arr[mid] < x:
low = mid + 1  # Search in the right half

else:
high = mid - 1  # Search in the left half

arr = np.sort(arr) #Sort the above array
start_time = time.time_ns()  # Start time measurement

result = binary_search(arr, x)
if result != -1:
print("Element is present at index", str(result))

else:
print("Element is not present in array")

end_time = time.time_ns()  # End time measurement

# Calculate and print time complexity
time_taken = end_time - start_time
print("Time complexity: O(log n) ā", time_taken, "nanoseconds")```

Element is present at index 405912
Time complexity: O(log n) ā 0 nanoseconds

### Pros and Cons of Binary Search

#### Pros:

• Extremely fast for large datasets: Because of its divide-and-conquer approach, the search time grows logarithmically with the list size, making it significantly faster than linear search for large data.
• Early termination: If the target isnāt present in the half being searched, it can be discarded immediately, saving time.

#### Cons:

• Requires sorted list: The list must be organized in ascending or descending order for the algorithm to work.
• More complex implementation: Compared to linear search, it requires a slightly more intricate coding structure.

Our learners also read: Free Python Course with Certification

## Linear Search vs Binary Search

While drawing a linear search vs binary search comparison, the parameters which are used will be working capacity, data structure, prerequisites, use cases, effectiveness, their denotation in a different type of complexities ( i.e. time and space) and their dry run.

### Working

• Linear search iterates through all the elements and compares them with the key which has to be searched.
• Binary search wisely decreases the size of the array which has to be searched and compares the key with mid element every time.

### Data Structure

• Linear search is flexible with all the data structures like an array, list, linked list, etc.
• Binary search cannot be performed on all data structures since we need multi-directional traversal. So data structures like the single linked list cannot be used.

### Prerequisites

• Linear search can be performed on all types of data, data can be random or sorted the algorithm remains the same. So no need for any pre-work to be done.
• Binary search works only on a sorted array. So sorting an array is a prerequisite for this algorithm.

### Use Case

• Linear search is generally preferred for smaller and random ordered datasets.
• Binary search is preferred for comparatively larger and sorted datasets.

### Effectiveness

• Linear search is less efficient in the case of larger datasets.
• Binary search is more efficient in the case of larger datasets.

### Time Complexity

• Time complexity for linear search is denoted by O(n) as every element in the array is compared only once. In linear search, best-case complexity is O(1) where the element is found at the first index. Worst-case complexity is O(n) where the element is found at the last index or element is not present in the array.
• In binary search, best-case complexity is O(1) where the element is found at the middle index. The worst-case complexity is O(log2n).

### Dry Run

Letās assume that we have an array of size 10,000.

• In a linear search, best-case complexity is O(1) and worst-case complexity is O(10000).
• In a binary search, best-case complexity is O(1) and worst-case complexity is O(log210000)=O(13.287).

## Top Data Science Skills to Learn

 Top Data Science Skills to Learn 1 Data Analysis Course Inferential Statistics Courses 2 Hypothesis Testing Programs Logistic Regression Courses 3 Linear Regression Courses Linear Algebra for Analysis

## Linear Search vs Binary Search Comparison Table

 Parameter Linear Search Binary Search Search Time Complexity O(n) ā Linear time complexity, where n is the number of elements. O(log n) ā Logarithmic time complexity, efficient for large datasets. Sorted Requirement Not required, can search in unsorted arrays. Required, operates only on sorted arrays or lists. Search Strategy Sequentially checks each element until a match is found. Divides the search interval in half based on comparison. Performance Slower for large datasets due to sequential scanning. Faster for large datasets due to logarithmic partitioning. Best Case O(1) ā If the element is found at the beginning. O(1) ā If the element is found at the middle. Worst Case O(n) ā If the element is at the end or not present. O(log n) ā If the element is not present or at extremes. Space Complexity O(1) ā Constant space, no extra space required. O(1) ā Constant space, iterative implementation. Usage Suitable for small datasets or unsorted data. Suitable for large sorted datasets where efficiency matters.

## Linear search vs binary search example:

In a large sorted array of 1000 elements, linear search requires up to 1000 comparisons to find an element, whereas binary search requires up to 10 comparisons due to its logarithmic nature (log2(1000)).

## Conclusion

Weāve understood the importance of data accessing in arrays, understood algorithms of the linear search and binary search. Walked through the codes of linear search and binary search. Compared the differences between linear search and binary search, made a dry run for a large-sized example.

Now that you are aware of the differences between linear search and binary search, try running both codes for a large sied dataset and compare the execution time, start exploring different searching algorithms, and try implementing them!

If you are curious to learn about data science, I suggest to check out IIIT-B & upGradās PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

Learn data science courses online from the Worldās top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

#### Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.
Get Free Consultation

Select
Select Area of interest
Select Work Experience
By clicking 'Submit' you Agree to
UpGrad's Terms & Conditions

## Frequently Asked Questions (FAQs)

1Compare linear search and binary search using their complexities.

Binary Search is more optimized and efficient than Linear Search in many ways, especially when the elements are in sorted order. The reason boils down to the complexities of both the searches.
Linear Search
1. Time Complexity: O(N) - Since in linear search, we traverse through the array to check if any element matches the key. In the worst-case scenario, the element will be present at the end of the array so we have to traverse through the end, and hence the time complexity will be O(N) where N is the total number of array elements.
2. Space Complexity: O(1) - We are not using any extra space so the space complexity will be O(1).
Binary Search
1. Time Complexity: O(log N) - In Binary Search, the search cuts down to half as we only have to look up to the middle of the array. And we are constantly shortening down our search to the middle of the section where the element is present.
2. Space Complexity: O(1)
- We are not using any extra space so the space complexity will be O(1).

2Is there any other method to search an element in an array?

Although linear search and binary search are often used for searching, there indeed is another searching method- the interpolation method. It is an optimized version of Binary Search where all the elements are distributed uniformly.
The idea behind this method is that in binary search, we always look up to the middle of the array. But in this method, the search can go to different locations depending on where the key is located. For instance, if the key is located near the last element of the array, the search will start from the end of the array.

3What are the different time complexities of interpolation search?

The worst-case time complexity of interpolation search is O(N) since in the worst case, the key will be at the end of the array so the iterator has to traverse throughout the array.
The average case complexity will be O(log(log N) since the element can be anywhere in the array. It may be near the starting point too.
The best-case complexity will be O(1) as, in the best case, the key will be the very first element of the array.

4Which is faster, linear or binary search?

Binary search is generally faster than linear search for large datasets due to its logarithmic time complexity š ( log ā” š ) O(logn), which efficiently reduces the search space by half with each comparison, compared to linear search's š ( š ) O(n) time complexity, which sequentially checks each element until a match is found or all elements are examined.

## Suggested Blogs

57597
Introduction The priority queue in the data structure is an extension of the ānormalā queue. It is an abstract data type that contains a

15 Jul 2024

142460
Association Rule Mining in data mining, as the name suggests, involves discovering relationships between seemingly independent relational databases or

13 Jul 2024

101802
Why data mining techniques are important like never before? Businesses these days are collecting data at a very striking rate. The sources of this eno

12 Jul 2024

58170
Pandas is a BSD-licensed and open-source Python library offering high-performance, easy-to-use data structures, and data analysis tools. The full form

11 Jul 2024

99419
Data types are an essential concept in the python programming language. In Python, every value has its own python data type. The classification of dat

11 Jul 2024

16859
Introduction to Data Mining In its raw form, data requires efficient processing to transform into valuable information. Predicting outcomes hinges on

04 Jul 2024

82932
What is a Data Analytics Lifecycle? Data is crucial in todayās digital world. As it gets created, consumed, tested, processed, and reused, data goes

04 Jul 2024

10560
Introduction Data structures are one of the most fundamental concepts in object-oriented programming. To explain it simply, a data structure is a par