In this PHP program, we will learn how to implement the Linear search algorithm to find an element that is there or not in an array. An array is a set of elements stored in continuous positions. Although there are many searching methods, linear search is the simplest technique. With the use of a for loop
and If condition
we could demonstrate this easy PHP program. For a beginner, we will strongly recommend going through the following topics for easy programming.
The linear search algorithm is also called a Sequential search algorithm because the element to be searched will sequentially compare with the elements of the array. This means comparing an element with the elements of the array one by one from the starting index to the last. It is the basic method used for searching an element in an array, list, or any other data structure. There will be an array of elements and one element to be searched, whether it belongs to the given set of elements. Linear search will traverse the array from start to end and, If a match is found it will output the index of array element where it got the match otherwise it gives an output ' item not found in the set '.
Unlike Binary search, Linear search is widely used to search an element in an unordered list i.e., the list containing elements that are not sorted. Although it is suitable for a smaller list (<100). Suppose there is a 10,000 element list and search element is available at the last position, sequential search will consume much time by comparing with each element of the list. Thus the worst-case time complexity of the linear search algorithm is O(n).
With a simple for loop
and If condition
program is explained here.
for loop
.for loop
, compare element to be searched with the current array element, and -echo
the index of the corresponding array element and set a search flag with value 1 also break
the loop.For a better understanding let's see an example,
Here is an array (7,2,9,6,1,3) and we need to search whether the number 6 is present in the array or not. Now iterate the array using a for loop
from starting index to last, and check whether the array element is equal to search element 6. The comparison will start with 7 and continues with 2,9, and then with 6 itself. Here we got a match at index 4 of the array. If got a match then echo
the index otherwise echo
the message “element not found”.
STEP 1: Initialize the array arr with some predefined values.
STEP 2: Define variable item with the search value and flag with a zero search status
STEP 3: Start a for loop
from zero to the size of the array for comparing each element in the array.
STEP 4: Use an if condition
to check whether the element is equal to the search element, and
STEP 5: If the condition is true then echo
the current array index and change the flag to 1, means we got our search element in the array, so break the for loop
STEP 6: otherwise for loop
will go for the next iteration until the end of the array.
STEP 7: Check the flag value is still 0, which means no match was found.
<?php
$arr = array(7,2,9,6,1,3); // given array
$item = 6; // value to be searched
$n = sizeof($a); //size of array
$flag=0; //search staus
// Going through array sequencially
for ($i = 0; $i < $n; $i++)
{
if ($arr[$i] == $item)
{
echo "<br>" , "Element is present at index " , $i+1 ;
$flag=1;
break;
}
}
if ($flag == 0)
echo "<br>" , "Element is not present in the array";
?>
Element is present at index 4