Starting with search algorithms
Tags: algorithms, searching, typescript
Published: 2021-08-26 18:18:28

---
title: Starting with search algorithms
published: true
description: Search algorithms that are used to retrieve information from a data structure, in this post I'll describe 3 search algorithms to find an element in lists
tags: algorithm,searching,typescript
series: Searching Algorithms
//cover_image: https://direct_url_to_image.jpg
canonical_url: https://ricardoborges.dev/starting-with-search-algorithms
---

Search algorithms that are used to retrieve information from a data structure, in this post I'll describe 3 search algorithms to find an element in lists.

### Linear Search

Linear search is the most basic and easier to understand search algorithm, it simply starts from the first element in the list, and check each element to see if it is the element we are searching for, if the element is found returns its position in the list, else returns `-1`, `null` or `undefined`. Since it goes through the entire list, its time complexity is _O(n)_.

```typescript
function linearSearch(list:number[], n:number): number {
  for(let i=0; i< list.length; i++) {
    if(list[i] === n) return i
  }
}

const list = [2,6,4,8,9,0,1,5]
console.log(linearSearch(list, 1)) // 6
```

What if we are given a [sorted](https://ricardoborges.dev/starting-with-sorting-algorithms) list? The next two algorithms work on sorted lists and are faster than the Linear Search algorithm.

### Jump Search

Instead of check element-by-element, This algorithm search block-by-block, so it checks fewer elements.

1. We start defining a block size
2. Starting from the beginning of the list, we check the last element of each block, if the element is smaller than the value we are looking for, we _jump_ to the next block.
3. But, if the last element of the current block is greater than the value we are looking for, we do a linear search in that block.

Jump Search time complexity in the average and worst cases is _O(√ n)_.

```typescript
function jumpSearch(list:number[], n:number): number {
  const blockSize = Math.floor(Math.sqrt(list.length))
  const start = 0
  
  while(list[start+blockSize-1] < n) {  	
    if(start + blockSize >= list.length) {
    	start += list.length - 1 - start
    }else {
    	start += blockSize
    }
  }
  
  for(let i=start; i < start+blockSize-1; i++) {
  	if(list[i] === n) return i
  }
}

const sortedList = [1,2,3,4,5,6,7,8,9,10]
console.log(jumpSearch(sortedList, 5)) // 4
```

### Binary Search

This algorithm is also meant for [sorted](https://ricardoborges.dev/starting-with-sorting-algorithms) lists, it uses a divide-and-conquer technique, which means it divides the problem into smaller parts that are easier to solve.

1. Start by checking the element in the middle of the lists, if this element is the element we are searching for returns its position.
2. If that middle element is greater than the element we are searching for, repeat the first step for the right half.
3. But, if that middle element is smaller, repeat the first step for the left half.

Notice that this algorithm keeps dividing the sublists into even smaller ones checking only the element in the middle of each sublist, and it might, eventually, wind up with a sublist that contains a single element. Since this algorithm is halving the list at each iteration its time complexity is _O(log n)_.

Worth mentioning that this algorithm is also implemented for a [Binary Search Tree](https://ricardoborges.dev/data-structures-in-typescript-binary-search-tree).

Here's an recursive implementation of Binary Search without any auxiliary data structures:

```typescript
function binarySearch(list:number[], n:number, low:number, high:number): number {
  // base case
  if(low > high) return
  
  let middle = Math.floor((low + high) / 2)
  
  if(list[middle] === n) return middle
  
  if(n < list[middle]) return binarySearch(list, n, low, middle-1)
  
  if(n > list[middle]) return binarySearch(list, n, middle+1, high)
}

const sortedList = [1,2,3,4,5,6,7,8,9,10]
console.log(binarySearch(sortedList, 3, 0, sortedList.length - 1))
```