# Linear Search vs. Binary Search

The difference between linear search and a binary search is that in linear search each element is checked and compared and then sorted whereas in binary search a list that is to be sorted is divided into two parts and then sorted. Searching and sorting are two main concepts in computer programming. Many algorithms are used for searching and sorting, but the two most used algorithms for searching and sorting are linear search and binary search. The difference between the linear search and a binary search is the working and efficiency of both algorithms. Binary search is a much more efficient algorithm as compared to the linear search algorithm. The iteration or the time it takes to compare each value before sorting is less in binary search as compared to linear search.

Linear search is a very complex algorithm if you want to search a number in a list, compare and iterate sometimes the number of values in the list. One by one each element of the list is retrieved and compared with the adjacent element. All the elements are accessed and check and then the right element is found. There can be the worst case if the last number in the list is the number that is to be searched. Linear search is the method by which the array is traversed and the element to be searched is founded.  If we talk about the efficiency, the efficiency is the number of times the program has to run to find the number. If we find the number we are looking for in the first position then only one comparison has to be made, and things are sorted but if not then comparisons have to make again and again, and memory is wasted. On average, the number of comparisons will be (n+1/2). And the worst-case efficiency of this technique is O (n) stands for the order of execution.

As compared to the linear search, binary search is very efficient. In this method, the array is divided into two-part and in this way number of comparisons is very less as compared to binary search. The time is also less in binary search as compared to linear search.  Binary search work in the way that the middle element of the array is found and then the middle element is compared with one part of array.  There can be three possibilities that are middle number can be the number we need to find or the number that is less than the middle number or the number that is greater than the middle of the middle number. The number of comparisons is at most log (N+1). Binary Search as compared to linear search is an efficient algorithm when compared to linear search, but the array has to be sorted before doing the binary search.

## Contents: Difference between Linear Search and Binary Search

### Comparison Chart

 Basis Linear Search Binary Search Meaning Linear search each element is checked and compared and then sorted Binary search a list that is to be sorted is divided into two parts and then sorted. Time Complexity The time complexity of the linear search is O(N). The time complexity of the binary search is O(log 2 N) Type of Algorithm Linear search is iterative. Binary search is Divide and conquer. Line of code In a linear search, we need to write more code. In a binary search, we need to write less code.

Linear Search

Linear search is a very complex algorithm if you want to search a number in a list, compare and iterate some times the number of values in the list. One by one each element of the list is retrieved and compared with the adjacent element. All the elements are accessed and check, and then the right element is found. There can be the worst case if the last number in the list is the number that is to be searched. Linear search is the method by which the array is traversed and the element to be searched is founded.  If we talk about the efficiency, the efficiency is the number of times the program has to run to find the number. If we find the number we are looking for in the first position then only one comparison has to be made, and things are sorted but if not then comparisons have to make again and again, and memory is wasted. On an average, the number of comparisons will be (n+1/2). And the worst case efficiency of this technique is O (n) stands for the order of execution.

### Binary Search

As compared to linear search, binary search is very efficient. In this method, the array is divided into two part and in this way number of comparisons is very less as compared to binary search. The time is also less in binary search as compared to linear search.  Binary search work in the way that middle element of the array is found and then the middle element is compared with one part of the array.

There can be three possibilities that are middle number can be the number we need to find or the number that is less than the middle number or the number that is greater than the middle of the middle number. The number of comparisons is at most log (N+1). Binary Search as compared to linear search is an efficient algorithm when compared to linear search, but the array has to be sorted before doing the binary search.

### Key Differences

1. Linear search each element is checked and compared and then sorted whereas Binary search a list that is to be sorted is divided into two parts and then sorted.
2. The time complexity of linear search is 0(N) whereas Time complexity of binary search is O(log 2N).
3. Linear search is iterative whereas Binary search is Divide and conquer.
4. In linear search, we need to write more code whereas in binary search we need to write less code.

### Conclusion

In this article above we see the clear difference between linear search and binary search.