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 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教