Advertisement
FaycaLProgrammer

linearSearch_startimes

Sep 21st, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.51 KB | Source Code | 0 0
  1. <?php
  2.  
  3. function linearSearch($arr, $n , $x){
  4.     for ($i = 0 ; $i < $n ;$i++)
  5.     {
  6.         if($arr[$i] == $x)
  7.             return $i;
  8.     }
  9.  
  10.     return -1;
  11. }
  12.  
  13. $A = array(17, 3, 45, 8, 29, 12, 56, 4, 23, 9, 31, 5, 48, 27, 1, 15, 36, 19, 7, 50);
  14. $N = count($A);
  15. $X = 31;
  16.  
  17. // print arr
  18. for ($i = 0 ; $i < $N; $i++)
  19.     echo $A[$i]." ";
  20.  
  21. echo "<br></br>";
  22.  
  23. $result = linearSearch($A,$N, $X);
  24.  
  25. if($result != -1)
  26.     echo "th value $X is found in $result ";
  27. else
  28.     echo "the value $X not found " ;
  29.  
  30. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement