Exercise: Linear Search

Exercise 7 Very easy

Prerequisites for the exercise

  1. PHP Functions
  2. PHP Control Flow
  3. All previous chapters

Objective

Create a function to search for a given value in an array.

Description

Suppose we have a given array and want to check whether it has a given item in it or not.

This can easily be done by sequentially iterating over the array and comparing each subsequent element with the item to find.

An algorithm that operates in this manner in order to search for a value in a array is generally known as a sequential search, or linear search, algorithm.

The term 'linear' comes from the fact that the running time of this algorithm can be expressed as a linear function on the length of the array.

In this exercise, you have to create a function linear_search() that implements the linear search algorithm in order to search for an item in a given array.

Its general form should be as follows:

function linear_search($arr, $target) {
   // Code here...
}

Two arguments should be provided to the function — the first is the array where to search for the target item, while the second is the item itself.

The function should return true if the target exists in the array, or else false.

Note that the function should search exactly for the target in the array.

Shown below are a couple of examples of the usage of the function:

<?php

function linear_search($arr, $target) { /* ... */ }

var_dump(linear_search([1, 2, 3], 2));
var_dump(linear_search([1, 2, 3], '2'));
var_dump(linear_search(['2', '4', '6'], '2'));
var_dump(linear_search(['2, 6', '1, 4'], '2'));
var_dump(linear_search([false, false, false, true], false));
bool(true) bool(false) bool(true) bool(false) bool(true)
View Solution

New file

Inside the directory you created for this course on PHP, create a new folder called Exercise-7-Linear-Search and put the .php solution files for this exercise within it.

Solution

Since we need to go over each and every item of the given array in search of the item to find, we ought to use for.

The basic setup of for is shown below:

<?php

function linear_search($arr, $target) {
   for ($i = 0; $i < count($arr); $i++) {
      // Code here.
   }
}

$i is a counter variable to keep track of the index of each new item in the array. It's initialized to 0. The condition $i < count($arr) makes sure that the loop iterates over the entire length of $arr. The last expression $i++ simply increments $i to point to the next item in the list.

With this in place, what needs to be done next is everything inside the loop. In each iteration, we need to compare $arr[$i] with $target via the === operator and return true from the function if both the values match.

However, if there is no match upto the end of the array, then nothing would be returned from within the loop and hence the loop would reach its end as usual. It's at this point, when execution moves out of the loop to the next statement, that we return false to signal the fact that no match was found for $target in the loop.

Altogether, we arrive at the following code:

<?php

function linear_search($arr, $target) {
   for ($i = 0; $i < count($arr); $i++) {

      // If a match is found, return true.
      if ($arr[$i] === $target) {
         return true;
      }
   }

   // No match was found, hence return false.
   return false;
}