Linear Search vs Binary Search: Difference Between Linear Search & Binary Search | upGrad blog (2024)

In my journey through data structures, I’ve navigated the nuances of Linear search Vs Binary search, 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<n; i++)
if(arr[i]==k)
return i+1;
return1;
}
public static void main(String[] args){
int[] arr=new int[]{1,2,5,6,3,8,9,9,0,13,45,65};
int k=13;
int n=arr.length;
int r=linear_search(arr, n, 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.

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

return -1 # Element not found

# 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;
}
return1;
}
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

return -1 # Element not found

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

ParameterLinear SearchBinary Search
MethodSequential comparisonDivide and conquer
Time ComplexityO(n)O(log n)
Sorted list required?NoYes
Efficiency for large datasetsSlowVery fast
Implementation complexityLowMedium
Ideal use casesSmall lists, unsorted dataLarge, sorted lists

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 Sciencewhich 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.

Linear Search vs Binary Search: Difference Between Linear Search & Binary Search | upGrad blog (2024)
Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 5819

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.