Article
Search algorithms

Search algorithms

25 November 2020

Linear search


The simplest algorithm is to cycle through the elements of the array (slice), comparing each element with the one we are looking for.
Algorithmic complexity is linear O(N).

Go example

package main

import "fmt"

func search(data []int, search int) int {
   for i := range data {
      if data[i] == search {
         return i
      }
   }

   return -1
}

func main() {
   testData := []int{7, 2, -5, 3, 1, 9, 8}

   fmt.Println(search(testData, 9)) // 5
   fmt.Println(search(testData, 4)) // -1
}


PHP example

function search(array $data, int $search): int
{
    for ($i = 0; $i < count($data); $i++) {
        if ($data[$i] === $search) {
            return $i;
        }
    }

    return -1;
}

$testArray = [7, 2, -5, 3, 1, 9, 8];

echo search($testArray, 9) . PHP_EOL; // 5
echo search($testArray, 4) . PHP_EOL; // -1

 

 

Binary search


Works only with sorted arrays (slices). Determine the minimum and maximum indices, then calculate the index between them (the middle). If the sought value is less than the middle value, we discard the right part, if it is more, we discard the left part. Then we repeat the steps recursively.
Algorithmic complexity is logarithmic O(log N).

Go example

package main

import "fmt"

func search(data []int, search int) int {
   minIndex := 0
   maxIndex := len(data) - 1

   for minIndex <= maxIndex {
      middleIndex := (minIndex + maxIndex) / 2

      if search < data[middleIndex] {
         maxIndex = middleIndex - 1 // Discard the right side
      } else if search > data[middleIndex] {
         minIndex = middleIndex + 1 // Discard the left side
      } else {
         return middleIndex
      }
   }

   return -1
}

func main() {
   testData := []int{-4, -2, 0, 1, 4, 6, 7, 11}

   fmt.Println(search(testData, 6))  // 5
   fmt.Println(search(testData, -3)) // -1
}


PHP example

function search(array $data, int $search): int
{
    $minIndex = 0;
    $maxIndex = count($data) - 1;

    while ($minIndex <= $maxIndex) {
        $middleIndex = intval(($minIndex + $maxIndex) / 2);

        if ($search < $data[$middleIndex]) {
            $maxIndex = $middleIndex - 1; // Discard the right side
        } else if ($search > $data[$middleIndex]) {
            $minIndex = $middleIndex + 1; // Discard the left side
        } else {
            return $middleIndex;
        }
    }

    return -1;
}

$testArray = [-4, -2, 0, 1, 4, 6, 7, 11];

echo search($testArray, 6) . PHP_EOL;  // 5
echo search($testArray, -3) . PHP_EOL; // -1

 

 

Interpolation search


It works only with sorted arrays (slices). This algorithm is similar to the binary algorithm with one exception - it tries to guess the location of an element.
Algorithmic complexity is logarithmic O(N) - worst, O(log(log N)) - expected.

Go example

package main

import "fmt"

func search(data []int, search int) int {
   minIndex := 0
   maxIndex := len(data) - 1

   for data[minIndex] <= search && data[maxIndex] >= search {
      middleIndex := minIndex + ((search-data[minIndex])*(maxIndex-minIndex))/(data[maxIndex]-data[minIndex])

      if search < data[middleIndex] {
         maxIndex = middleIndex - 1 // Discard the right side
      } else if search > data[middleIndex] {
         minIndex = middleIndex + 1 // Discard the left side
      } else {
         return middleIndex
      }
   }

   return -1
}

func main() {
   testData := []int{-4, -2, 0, 1, 4, 6, 7, 11}

   fmt.Println(search(testData, 7))  // 6
   fmt.Println(search(testData, -3)) // -1
}


PHP example

function search(array $data, int $search): int
{
    $minIndex = 0;
    $maxIndex = count($data) - 1;

    while ($minIndex <= $maxIndex) {
        $middleIndex = $minIndex + intval((($search - $data[$minIndex]) * ($maxIndex - $minIndex)) / ($data[$maxIndex] - $data[$minIndex]));

        if ($search < $data[$middleIndex]) {
            $maxIndex = $middleIndex - 1; // Discard the right side
        } else if ($search > $data[$middleIndex]) {
            $minIndex = $middleIndex + 1; // Discard the left side
        } else {
            return $middleIndex;
        }
    }

    return -1;
}

$testArray = [-4, -2, 0, 1, 4, 6, 7, 11];

echo search($testArray, 7) . PHP_EOL;  // 6
echo search($testArray, -3) . PHP_EOL; // -1



Sources:
https://en.wikipedia.org/wiki/Linear_search
https://en.wikipedia.org/wiki/Binary_search_algorithm
https://en.wikipedia.org/wiki/Interpolation_search