×

Linear vs Binary Search: Data Structure

A linear search also referred as a sequential search. It is a way to find an element within a list and it scans a list without skipping any element or we can say it searches a whole list sequentially until a match is found or the whole list has been searched. Once element has found then it returns the position of that element in the list.

Example:

Linear vs Binary Search: Data Structure

Complexity of Linear Search

As linear search scans list elements one by one without skipping any element until a match is found or the whole list has been scanned. So, the time complexity of the linear search depends on the number of elements present in the list which O(n) where n is the number of elements present in the list in worst case scenario.

Binary search is another way to find an element in the list. In binary search, we calculate the middle element and check the middle element is greater or smaller than the element which we want to search in the list. Binary search algorithm doesn’t search the list sequentially or we can say it doesn’t scan each element present in the list. It searches a sorted list or array by dividing it in to the half. The binary search algorithm takes less time to search an element in list over the linear search algorithm.

For using the binary search algorithm, we need to consider one thing which is array or list must be sorted. The binary search uses the divide and conquers technique, in which array or list will be divided recursively.

Here we will try to understand cases used in Binary Search:

Case 1: element<array [mid] then left = mid+1.

Case 2: element>array [mid] then right=mid-1

Case 3: element = array [mid] // element is found

Example:

Linear vs Binary Search: Data Structure

Complexity of Binary Search         

The time complexity of binary search is O(log n) because in binary search, we search an element in half of list by dividing it recursively.

Difference Between Linear and Binary Search

ParametersLinear searchBinary search
Definition  A linear search also referred as a sequential search. It is way to find an element within a list and it scans a list without skipping any of element or we can say it searches a whole list sequentially until a match is found or the whole list has been searched.Binary search is another way to find an element in the list. In binary search, we calculate the middle element and check the middle element is greater or smaller than the element which we want to search in the list.
Sorted dataThere is no requirement of sorted data. The linear search can work on both sorted and unsorted data.In Binary Search, the array must be sorted then only we can apply the algorithm.
Technique  usedThe linear search is based on sequential technique.The binary search is based on the divide and conquers technique.
Worked onIt can be used on small data.It can be used for large data.
Algorithm efficiencyThe linear search is not efficient as compared to binary search.The binary search takes less time to search an element over the linear search.
Time complexity in worst-caseThe time complexity of the linear search depends on the number of elements present in the list which is O(n) where n is the number of elements present in the listThe time complexity of binary search is O(log n).

Related Topics

What is an AVL Tree in Data Structure?

AVL tree stands for (Adelson, Velskii, & Landis Tree) Data structure Data management is called database management. A data model is a system used to store, manage, and optimize computer resources. Data...

4 minutes read.

Types of Data Structures

Almost every programme or software system that has been built makes use of data structures. Furthermore, data structures are basics of computer science and software engineering. When it comes to...

7 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example - For the above diagram,...

3 minutes read.

Given a Perfect Binary Tree, Reverse Alternate Levels

Implementation //writing a program in C++ language to see how to approach it. #include <bits/stdc++.h> using namespace std; // creating a tree node. struct Nod { char ky; struct Nod *Lft, *Rt; }; // creating a new utility function...

9 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.

Length of longest palindrome in a linked list using O(1) extra space

Length of longest palindrome in a linked list using O(1) extra space In this problem, we need to find the length of the longest palindrome list that is present in given...

2 minutes read.

Symmetric binary tree

Implementation // writing a C++ program to check whether a given binary tree is symmetric or not. #include <bits/stdc++.h> using namespace std; // creating a binary tree node. struct __Nod { int ky; struct __Nod *Lft, *Rt; }; //...

4 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

4 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.

Delete the Middle element of the Linked List in C

Delete the Middle element of the Linked List in C This article has given a singly linked list and will delete the middle element of the given linked list. Example:  The given...

3 minutes read.

Huffman tree in Data Structures

The Huffman trees in the field of data structures are pretty impressive in their work. They are generally treated as the binary tree, which is linked with the least external...

6 minutes read.

Linear Queue Data Structure in C

Data Structure There are many ways to store data in programming, that Queue has features that make it all the more special. We all know that data structure is a way...

9 minutes read.

Recursion in Fibonacci

Fibonacci heap is considered to be a particular execution of the heap data structure that ultimately helps in making use of not just any number but the Fibonacci numbers. It...

3 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Given a Generate all Structurally Unique Binary Search Trees

Implementation // Creating a C++ program that will help us build all the binary search trees for the keys from 1 to n.  #include <bits/stdc++.h> using namespace std; // creating a structure that will...

8 minutes read.

Bubble Sort in Data Structures

Bubble Sort in C++ The bubble sort algorithm analyses two adjacent elements and swaps them until they are no longer in the desired order. Each iteration moves each member of the array...

4 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.