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

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.

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

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**

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; return –1; } 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.

**Binary Search**

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.

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?

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.

**Linear Search vs Binary Search**

**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**

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

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