Algorithm Bincike na Lissafi hanya ce ta asali kuma madaidaiciyar hanyar nema. Yana aiki ta hanyar ƙididdigewa ta kowane kashi na jerin don nemo takamaiman ƙima. Duk da yake mai sauƙi, wannan hanyar tana da tasiri ga ƙananan jeri ko lokacin da aka riga aka jera jerin.
Yadda Ake Aiki
- Maimaita Ta hanyar Abubuwa: Fara daga kashi na farko kuma bincika idan ƙimar ta yanzu ta yi daidai da ƙimar manufa.
- Bincika Match: Idan darajar a matsayi na yanzu ya dace da ƙimar manufa, aikin bincike ya ƙare, kuma an mayar da matsayin darajar.
- Matsar zuwa Abu na gaba: Idan ba a sami madaidaici ba, matsa zuwa kashi na gaba kuma ci gaba da dubawa.
- Maimaita: Maimaita matakai na 2 da 3 har sai an sami ƙimar ko duk jerin sun wuce.
Misali: Neman Lissafin Lamba 7 a cikin Tsari
function linearSearch($arr, $target) {
$n = count($arr);
for($i = 0; $i < $n; $i++) {
if($arr[$i] == $target) {
return $i; // Return the position of the value
}
}
return -1; // Value not found
}
$array = [2, 5, 8, 12, 15, 7, 20];
$targetValue = 7;
$result = linearSearch($array, $targetValue);
if($result != -1) {
echo "Value $targetValue found at position $result.";
} else {
echo "Value $targetValue not found in the array.";
}
A cikin wannan misalin, muna amfani da hanyar Neman Lissafi don nemo ƙimar 7 a cikin tsararrun da aka bayar. Muna maimaita ta kowane kashi na tsararru kuma muna kwatanta shi tare da ƙimar manufa. Lokacin da muka sami darajar 7 a matsayi na 5, shirin ya dawo da saƙon "Value 7 samu a matsayi