doubi4617
doubi4617
2016-07-16 10:55

线性或二进制搜索?? -PHP

已采纳

I have some code that I wrote and it works really well. Except I am not sure whether what i have written is linear or bineary searching?! I get really confused about the differences. Could someone please clarify the differences and what my code is so I can explain it to someone?

-The code below searches a value inputted by the user. And goes through a csv file of data. I then saves all values into a new array which has the results. Hopefully that makes sense.

I just want to know whether my code is linear or binary? I get so confused about them *

$SearchThis = isset($_POST['Search']) ? $_POST['Search'] : '';
//empty()
$SearchThis = !empty($_POST['Search']) ? $_POST['Search'] : '';



// Grabs the csv file (and its existing data)  and makes it into an array
$csv = array();
$lines = file('data/StaffData.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
    $csv[$key] = str_getcsv($value);
}



//A new array which will display the search results
$new_csv = array(); 

//This displays which rows have matched the search (it is put in an array)

//Looks through full names
$keys = array_keys(array_column($csv, 0), $SearchThis);  //  original code
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array
}
//Looks through phone numbers
$keys = array_keys(array_column($csv, 1), $SearchThis);  // original code
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array
}
//Looks through gender
$keys = array_keys(array_column($csv, 2), $SearchThis);  // original code
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array
}
//Looks through Birthday
$keys = array_keys(array_column($csv, 3), $SearchThis);  // original code
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array
}

//Looks through Type of work
$keys = array_keys(array_column($csv, 4), $SearchThis);  // original code
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • donglu9445 donglu9445 5年前

    your code is doing a linear search , a binary search require that the data is sorted , in your case its a regular file , plus in a binary search you start the search from the middle and compare the value your searching with the value in the middle and decide whether to go left or right depending on how your data is sorted . i hope it helped a little.

    点赞 评论 复制链接分享

相关推荐