线性或二进制搜索?? -PHP
Linear or Binary Searching?? -PHP
我写了一些代码,它运行得非常好。除了我不确定我写的是线性搜索还是二进制搜索?!我对这些差异感到非常困惑。有人可以澄清差异以及我的代码是什么,以便我可以向其他人解释吗?
-下面的代码搜索用户输入的值。并通过一个 csv 数据文件。然后我将所有值保存到一个包含结果的新数组中。希望这是有道理的。
我只想知道我的代码是线性的还是二进制的?我对他们很困惑 *
$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
}
您的代码正在进行线性搜索,二分搜索需要对数据进行排序,在您的情况下它是一个常规文件,另外在二分搜索中,您从中间开始搜索并将搜索的值与中间的值,并根据数据的排序方式决定向左还是向右。
希望对您有所帮助。
我写了一些代码,它运行得非常好。除了我不确定我写的是线性搜索还是二进制搜索?!我对这些差异感到非常困惑。有人可以澄清差异以及我的代码是什么,以便我可以向其他人解释吗?
-下面的代码搜索用户输入的值。并通过一个 csv 数据文件。然后我将所有值保存到一个包含结果的新数组中。希望这是有道理的。
我只想知道我的代码是线性的还是二进制的?我对他们很困惑 *
$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
}
您的代码正在进行线性搜索,二分搜索需要对数据进行排序,在您的情况下它是一个常规文件,另外在二分搜索中,您从中间开始搜索并将搜索的值与中间的值,并根据数据的排序方式决定向左还是向右。 希望对您有所帮助。