The difference between a linear search and a binary search

Linear search and binary search are two methods used in arrays for search elements. A search is a process of finding an item in a list of items store in any order or randomly.

The main difference between a linear search and a binary search is that a binary search takes less time to search for an element from a sorted list of elements. Thus, it is concluded that the efficiency of the binary search method is higher than the linear search.

Another difference between the two is that there is a precondition for binary search, ie. Elements must be sorted until there is such a prerequisite in linear search. Although both search methods use different techniques discussed below.

Comparison table

Basis for comparison Linear search Binary search
The complexity of time ON THE) O (log 2 N)
Best case First element O (1) Central element O (1)
Prerequisite for string It is not necessary The string must  order
Worst case for N number of elements N comparisons  require It can be concluded after only a 2 N comparison log
Can  applie to String and linked list Cannot  apply directly to linked lists
Insert operation It is easily inserted at the end of the list Request processing to insert it in the appropriate place to maintain the sorted list.
Algorithm type Iterative in nature Share and conquer in nature
Usefulness Easy to use and no need for any arranged elements. In any case, the cunning algorithm and elements should be organized in order.
Code lines Less More

Definition of linear search

In a linear search, each element of the array is downloaded one by one in a logical order and checks whether it is the desired element or not. The search will fail if all elements are accessed and the desired element is not found. In the worst case, we may need to scan half the size of the array (n / 2) in the average case number.

Therefore, linear search can be defined as a technique that sequentially crosses a string to locate a given item. The program given below illustrates searching for a string element using a search.

Linear search efficiency

The amount of time or number of comparisons made in searching records in a search table determines the efficiency of the technique. If the desired record is present in the first place of the search table, then only one comparison is made. When the desired record is the last, then n comparisons should be made.

If the record is present somewhere in the search table, the average will be the number of comparisons (n ​​+ 1/2). The worst case of efficiency of this technique is O (n) means the order of execution.

C Element search program using the linear search technique.

#include #includevoid main () {int a [100], n, i, item, loc = -1; clrscr (); printf (“nEnter element number:”); scanf (“% d”, & n); printf (“Enter numbers: n”); for (i = 0; i <= n-1; i ++) {scanf (“% d”, & a [i]); } printf (“nEnter search number:”); scanf (“% d”, & item); for (i = 0; i <= n-1; i ++) {if (item == a [i]) {loc = i; Pause; }} if (loc> = 0) {printf (“n% d found at location% d:”, item, loc + 1); } else {printf (“n Item does not exist”); } getch (); }

Definition of binary search

Binary search is an extremely efficient algorithm. This search technique takes less time to search for a given item with the least possible comparison. To perform a binary search, we must first sort the elements of the array.

The logic behind this technique is given below:

  • First find the middle element of the array.
  • The middle element of the array is compared to the element to be searched.

There are three cases:

  1. If an item is a required item, then the search is successful.
  2. When the element is smaller than the desired item, search only the first half of the array.
  3. If it is larger than the desired element, search in the second half of the string.

Repeat the same steps until the item is found or exhausted in the search area. In this algorithm, each time the search area decreases. Therefore, the number of comparisons is at most log (N + 1). As a result, it is an efficient algorithm compared to linear search, but the array must be sorted before binary search.

C Program to find an element using the binary search technique.

#include void main () {int i, beg, end, middle, n, search, arrai [100]; printf (“Enter element number n”); scanf (“% d”, & n); printf (“Enter% d numbers n”, n); for (i = 0; i <n; i ++) scanf (“% d”, & string [i]); printf (“Enter search number n”); scanf (“% d”, & search); beg = 0; end = n – 1; middle = (beg + end) / 2; vhile (beg <= end) {if (string [middle] <search) beg = middle + 1; otherwise if (string [middle] == search) {printf (“Search was successful. n% d found at% d. n”, search, middle + 1); Pause; } otherwise end = middle – 1; middle = (beg + end) / 2; } if (beg> end) printf (“Search failed!% d not present in the list. n”, search); getch (); }


Both linear and binary search algorithms can be useful depending on the application. When a series of data structures and elements are arranged in an ordered order, then binary search is quickly sought in search. If a list of data structures is linked, regardless of how the elements arrange, a linear search is adopted due to the unavailability of direct implementation of the binary search algorithm.

A typical binary search algorithm cannot use on a link list, because the linked list is dynamic in nature and it is not known where the middle element is actually assigned. Therefore, there is a requirement to design a variation of the binary search algorithm that can also work on a linked list, because binary search is faster to execute than linear search.

Also read: CBD for back pain