C++ c++;排序数组的二进制搜索函数故障(常用名称搜索)
下面是我的代码和文本文件(忽略注释掉的行)。我无法使二进制搜索方法正常工作。我有一个排序向量。我甚至有一个for语句来打印二进制搜索方法中使用的检查和for循环中的检查,print语句说它应该返回true。谢谢你的帮助!对于代码其他部分的提示/改进,我们也非常感激,谢谢C++ c++;排序数组的二进制搜索函数故障(常用名称搜索),c++,vector,binary-search,C++,Vector,Binary Search,下面是我的代码和文本文件(忽略注释掉的行)。我无法使二进制搜索方法正常工作。我有一个排序向量。我甚至有一个for语句来打印二进制搜索方法中使用的检查和for循环中的检查,print语句说它应该返回true。谢谢你的帮助!对于代码其他部分的提示/改进,我们也非常感激,谢谢 #include <iostream> #include <fstream> #include <vector> #include <string> #include <al
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<string> getVector(const string&);
string getName(const string&);
void selectionSort(vector<string>&);
bool search(const string&, const vector<string>&);
int main()
{
string boyName, girlName;
bool boyNameFound, girlNameFound;
vector<string> boyNames(getVector("BoyNames.txt"));
vector<string> girlNames(getVector("GirlNames.txt"));
/* debugging
for (vector<string>::const_iterator i = boyNames.begin(); i != boyNames.end(); ++i)
{
cout << *i << endl;
}
for (vector<string>::const_iterator i = girlNames.begin(); i != girlNames.end(); ++i)
{
cout << *i << endl;
}
*/
boyName = getName("boy's"); //getting the name of a boy and setting the var boyName = to it
//girlName = getName("girl's");
selectionSort(boyNames); //sort vector of popular boys names
//selectionSort(girlNames);
//boyNameFound = search(boyName, boyNames);
//girlNameFound = search(girlName, girlNames);
if(search(boyName, boyNames)){ cout << "boys true " <<endl;}
else{cout << "boys false"; }
// if(search(girlName, girlNames)){ cout << "girls true " <<endl;}
// else{cout << "girls false";}
}
vector<string> getVector(const string& ph){
vector<string> names;
ifstream namesFile(ph.c_str());
if (!namesFile){
cout << "Error opening file \n";
}
string line;
while(getline(namesFile,line)){
names.push_back(line);
}
names.push_back("end"); //to help when searching for names
return names;
}
string getName(const string& ph){
string name;
if(ph=="boy's"){ //when boys is selected
cout << "Enter a boys name or N to skip: ";
cin >> name;
}
else{ //when girls is selected
cout << "Enter a girls name or N to skip: ";
cin >> name;
}
return name;
}
bool search(const string& ph, const vector<string> &nameList){
bool nameInList;
cout << "The boys name we are searching for is " << ph <<endl; //printing out the name we are looking for. this is for debugging
cout << "TEST BINARY SEARCH: 0=false 1=true: " << binary_search (nameList.begin(), nameList.end(), ph) << endl;
if(binary_search (nameList.begin(), nameList.end(), ph)){
nameInList = true;
}
for(int y = 0; y < nameList.size(); y++)
{
cout << ph << " == " << nameList[y] << endl; //printing out for debugging
if (ph == nameList[y])
nameInList = true;
}
return nameInList;
}
void selectionSort(vector<string> &arr){ //selection sort works good
int startScan, minIndex;
string minValue;
for (startScan = 0; startScan < (arr.size() - 1); startScan++){
minIndex = startScan;
minValue = arr[startScan];
for(int index = startScan + 1; index < arr.size(); index++){
if (arr[index] < minValue){
minValue = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minValue;
}
}
请记住,
二进制搜索
仅对排序列表有效。它以两个变量l
和r
开始,这两个变量表示要搜索的名称必须位于左右边界之间
在每个步骤中,它都创建一个变量m=(l+r)/2
,并将该索引处的名称与正在搜索的名称进行比较。如果这个名字就是你要找的,你就完了。否则,如果名称更大,则将r
设置为m
并继续;如果较小,则将l
设置为m
这是我的密码:
int binarySearch(const vector <string> &names, string name){
int l = 0, r = names.size();
while(l != r){
int m = (l+r)/2;
if(names[m] == name){return m;}
else if(names[m] > name){r = m;}
else{
l = m;
}
}
return -1; // name does not exist in the list
}
这两种方法的中心思想相同,它们的运行时几乎相同。使用这些方法中的任何一种都应该适用于您的程序
您的程序还有一个函数selectionSort()
,我假设它应该在执行二进制搜索算法之前对std::vector
进行排序。注意C++提供了一个通用的排序函数,<代码> STD::SoTo()/Cuff>,它在很多情况下比选择排序运行得快得多。要使用std::sort()
,您必须在程序中的某个地方键入#include
。您是否测试了排序例程中的两个名称,例如Adam和Ben,然后是Ben和Adam?以JaredJeremyMar开头的部分。。。以…rcoBrysonMartin结尾看起来像一行文字。我不确定这是不是你想要的。我们可以假设,binary\u search
可以按预期工作。所以我猜,排序功能是罪魁祸首。我几乎100%确信排序功能工作正常。在调用二进制搜索之前,我打印出了排序的向量,我觉得它很好/排序。另外,如果你们看二进制排序下面,我有一个for循环,它在向量中循环,手动检查提供的名称,但也无法正确工作。您似乎返回了-1
。总是。因为其中只有一个return语句:return-1代码>你是对的。我将修改我的代码以返回正确的值。另外,当您执行此操作时,捕获向量作为常量引用,副本为O(N),函数为O(logN),完成得很好,+1。人们会对你的回报发表意见;格式化。我试过实现你的两个解决方案,但似乎都不起作用。您提供的第二个解决方案似乎从未进入if语句。
int binarySearch(const vector <string> &names, string name){
int l = 0, r = names.size();
while(l != r){
int m = (l+r)/2;
if(names[m] == name){return m;}
else if(names[m] > name){r = m;}
else{
l = m;
}
}
return -1; // name does not exist in the list
}
int curr = 0;
for(int i = names.size()/2; i >= 1; i /= 2){ // jump size
while(curr+i < names.size() && names[curr+i] <= name){
curr += i;
}
}
if(names[curr] == name){
// name was found at index curr
}