Search Algorithms
By: Siddharth Rout
Tags: Algorithms, Coding, Python, Teens, Data Science

A search algorithm is an algorithm which searches for an element in a data structure. Suppose you have an array, and you want to find the index location of an element within the array. There are two main ways that you can find the index of the element: linear search and binary search.

Linear Search

This is the method that most people would use when searching for an element. As the name suggests, this method involves iterating through every element in an array until you find the target element. This takes O(n) time in the worst case, where n is the length of the array.

Linear search algorithm, via GeeksForGeeks

The usual implementation of a linear search algorithm checks every element beginning with the first element, at index 0. It then proceeds right, or through the array, until the target element is found. Once the element is found, it returns the index position of the element.

If the element is not in the array, then -1 is returned.

This method of searching an array is one of the least efficient methods. The time taken to search through the elements of an array using linear search does not matter much when you are using small data structures (<100,000 elements). However, if you are using larger data structures, or when the time taken needs to be as low as possible, linear search is an extremely inefficient search algorithm.

The Python implementation of a linear search algorithm is below.

def linearSearch(arr, x):



length = len(arr)



for i in range(length):

if arr[i] == x:

return i



return -1

This algorithm looks at every index position in the array, and checks whether it matches with the target element (x). If it does, the index position is returned, and if the whole array is checked and the target element is not present in the array, -1 is returned.

We can slightly improve the runtime of this algorithm by moving inwards to the middle of the array instead of moving from left to right. This still takes O(n) time in the worst case (x is in the middle of the array/not in the array), but this takes a lesser amount of time if the target element is close to the end of the array.

def linearSearch(arr, x):



left = 0

right = len(arr) - 1



while left <= right:

if arr[left] == x:

return left

if arr[right] == x:

return right



left += 1

right -= 1



return -1

Interval Search

If the array that you are searching for an element in is unsorted, then you have to use linear search or one of its variants to find an element. If the array is sorted, however, you can use an interval search algorithm for finding an element.

There are many types of interval search, but none of them are as popular and ubiquitous as binary search. The “binary” in binary search does not refer to the counting system (look at my post on number systems here), but to splitting items in two.

Binary search works by repeatedly cutting an array in half. The first element it checks is not the first or last element in the array, but the one in the middle (rounded down if there are an even number of elements). If the target element is larger than the element in the middle, then it takes the end half of the array to the next stage, and if the target element is smaller than the element in the middle, it takes the first half of the array. This process is continued until the element in the middle of a subarray is the target element.

Binary search algorithm, via GeeksForGeeks

Below is the Python implementation of the binary search algorithm.

def binarySearch(arr, x):

low = 0

high = len(arr) - 1



while low <= high:

mid = (high + low) // 2



if arr[mid] < x:

low = mid + 1



elif arr[mid] > x:

high = mid - 1



else:

return mid



return -1

There are other types of interval search algorithms as well. One of my personal favorites is jump search, where you split the array into subarrays of length y, and check the last element of each subarray to see whether or not it is larger than the target element x. Once you find the subarray where the last element is equal to or larger than the target element, you can search the much smaller subarray with another search algorithm(s) of your choice for the target element.

import math



def jumpSearch(arr, x):



step = math.floor(math.sqrt(len(arr)))



prev = 0



while prev < len(arr) - 1:

if x <= arr[prev]:

break

else:

prev += step



index = 0

for i in arr[prev - step:prev + 1]:

if i == x:

return index + (prev - step)

else:

index += 1



return -1

The jump search algorithm above uses jump search once through the array to get a smaller subarray, and then uses linear search to find the target element within the subarray. However, you can use any other searching algorithm once you have the subarray, and if the subarray is large enough, you can use jump search again.

Those are the three main algorithms that can be used to find an element in an array. However, these are by no means the only algorithms available. There are many, many searching algorithms, such as Fibonacci Search, Interpolation Search, Exponential Search, and Ternary Search. Building another searching algorithm is not difficult. You just have to see things from a new perspective.

To demonstrate that creating a searching algorithm is not hard, I coded a simple middle-out linear search algorithm. This algorithm favors elements on the upper 4/6ths of the array. For example, if arr had a length of 6, the elements from arr[2] onwards would be found quicker than if you used traditional linear search.

def middle_out(arr, x):



if len(arr) % 2 == 0:

right = int(len(arr)/2)

left = right - 1

else:

mid = int((len(arr) - 1)/2)

if arr[mid] == x:

return mid

else:

left = mid - 1

right = mid + 1



while left >= 0:

if arr[left] == x:

return left

elif arr[right] == x:

return right

else:

left -= 1

right += 1



return -1

— — — — — -

If you enjoyed this article, please share it with your friends, family, and anyone else you think will be interested in this! I am a small blogger, and I rely on your support to grow my blog. Thank you so much!

My name is Sid, and I post an article every few weeks on interesting topics related to Computer Science, Science, and Mathematics! Thank you for reading this post!