doubi4617 2016-07-16 10:55
浏览 74
已采纳

线性或二进制搜索?? -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 2016-07-16 11:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?