Programs

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

Introduction

Contiguous memory allocation in programming languages provides a flexible implementation of storing multiple data points. This can be utilized at its peak if we want to segregate the data and merge all the similar data in a contiguous data structure like an array, list, etc.

Contiguous memory allocation has many implementations in real-world applications like an operating system in computers, database management systems, etc. This data structure is considered a flexible one because adding a new data point to an array just requires a single unit of time i.e.; O(1).

But the problem arises when we want to look at a particular entry or search for a particular entry because all the real-world applications rely on the data accessing commands. And this task must be quick enough to meet the speed of the processor and memory.

There are various search algorithms divided based on the number of comparisons we make to search the element.

If we compare each data point in the array to search an element then it is considered as a sequential search. But if we are comparing only a few elements by skipping some of the comparisons then it is considered as an interval searching. But we need an array to be in sorted order(ascending order or descending order) to perform an interval search on it. 

Also read: Free data structures and algorithm course!

The time complexity of the sequential search is linear O(n), and the time complexity of binary search(an example of interval search) is O(log n). Also, there are other searching algorithms like exponential search, jump search, etc.

Must read: Learn excel online free!

But linear search and binary search are used mostly, where the linear search is for random or unsorted data and binary search is for sorted and ordered data. Hashing is a special searching algorithm where the time complexity of accessing a data point is O(1).

Time complexity for linear search also refers to the fact that the element that has been searched live on the first memory block, which is why its complexity is O(1); it is also known as the best case complexity. Whereas O(n) is referred to as worst-case complexity where ‘n’ is the number of comparisons. In this, one has to look at every item before reaching the item that is the last. 

Therefore, the complexity of linear search algorithm is O(n).  Hope you have got the answer of what is the worst case time complexity of linear search algorithm. 

If you want to know more about linear search vs binary search or what is the worst case time complexity of linear search algorithm, then this article is for you. Let’s first walk through the algorithms of linear search and binary search and then compare the differences between linear search and binary search.

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. 

 

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

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

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.

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

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

Compare 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).

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

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

Want to share this article?

Prepare for a Career of the Future

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Data Science Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks