Programs

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

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

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 SearchProsÂ

• 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Â MethodÂ Sequential comparisonÂ Divide and conquerÂ Time ComplexityÂ O(n)Â O(log n)Â Sorted list required?Â NoÂ YesÂ Efficiency for large datasetsÂ SlowÂ Very fastÂ Implementation complexityÂ LowÂ MediumÂ Ideal use casesÂ Small lists, unsorted dataÂ Large, 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 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.