我正在为课程编写一个程序,要求我执行以下操作。将单词文件加载到字符串向量中,允许用户输入要搜索的单词,执行顺序搜索,使用“选择排序”对单词进行排序,最后执行二进制搜索。
除了二进制搜索之外,我设法让整个程序按预期工作。由于某些原因我无法理解,我的顺序搜索功能正确显示所有信息,而二进制搜索功能只显示初始cout。
感谢您的帮助!
以下是运行我的程序的当前输出的副本:
Enter the word to search for: YELL YELL was found successfully after 407 comparisons! Perform selection sort... Perform a binary search using selection sorted vector... Press any key to continue . . .
我的程序的代码(我知道没有人喜欢使用命名空间std,但我需要为我的课程这样做):
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
void loadWords(vector<string>& words); // load unsorted words from file to vector of strings.
int sequentialSearch(vector<string>& words, string targetWord, int& count); // Perform sequential search and keep track of how many comparisons have happened.
void selectionSort(vector<string>& wordsSorted); // Perform the selection sort on the words.
int binarySearch(vector<string>& wordsSorted, string targetWord, int& count); // Perform binary search and keep track of how many comparisons have happened.
int main()
{
vector<string> words;
vector<string> wordsSorted;
loadWords(words);
wordsSorted = words;
string targetWord;
int count = 0;
cout << "Enter the word to search for: " << endl;
getline(cin, targetWord);
sequentialSearch(words, targetWord, count);
selectionSort(wordsSorted);
binarySearch(words, targetWord, count);
return 0;
}
void loadWords(vector<string>& words)
{
ifstream inFile("unsortedBog.dat");
string word;
while (inFile >> word)
{
words.push_back(word);
}
inFile.close();
}
int sequentialSearch(vector<string>& words, string targetWord, int& count)
{
for (int i = 0; i < words.size(); i++)
{
count++;
if (words[i] == targetWord)
{
cout << targetWord << " was found successfully " << "after " << count << " comparisons!" << endl;
return i;
}
}
cout << "After performing " << count << " comparisons, " << targetWord << " could not be found." << endl;
}
void selectionSort(vector<string>& wordsSorted)
{
cout << "Perform selection sort... \n" << endl;
int min = 0;
string min1;
for (int i = 0; i < wordsSorted.size() - 1; i++)
{
if (wordsSorted[i] < wordsSorted[min])
{
min = i;
}
min1 = wordsSorted[i];
wordsSorted[i] = wordsSorted[min];
wordsSorted[min] = min1;
}
}
int binarySearch(vector<string>& wordsSorted, string targetWord, int& count)
{
cout << "Perform a binary search using selection sorted vector... \n" << endl;
int first = 0,
last = wordsSorted.size() - 1,
mid,
position = -1;
bool found = false;
while (!found && first <= last)
{
int i = 0;
count = i++;
mid = (first + last) / 2;
if (wordsSorted[mid] == targetWord) // If value is found at mid
{
found = true;
position = mid;
cout << "The target word was located successfully after performing " << count << " comparisons.";
return position;
}
else if (wordsSorted[mid] > targetWord) // Lower half
last = mid - 1;
else if (wordsSorted[mid] < targetWord) // Upper half
first = mid + 1;
else
cout << "After performing " << count << " comparisons, the target word could not be found.";
}
}
答案 0 :(得分:2)
二进制搜索需要排序数组。
你的selectionSort()
远远超过了排序数组的任务。
它唯一能做的就是在数组中搜索应该是排序列表中第一个单词的单词,将其移动到数组中的第一个单位,并宣告&#34;任务完成&#34;。数组的其余部分仍未排序。
由于binarySearch()
假设数组已经排序,因此它完全脱离了轨道。
P.S。只是为了让事情变得更糟,selectionSort()
无法首先搜索整个数组。它没有查看数组中的最后一个元素。如果按字典顺序排列的第一个单词位于数组的最后一个元素中,则无法找到它。
答案 1 :(得分:1)
自从我使用C ++以来已经很长时间了,但这就是我解决问题的方法。请原谅我的代码效率低下,因为我只是想要一个通用的解决方案。
我注意到的一些问题是:
您的选择排序不起作用。根据我的记忆,大多数这些效率低下的排序算法需要两个循环,一个用于跟踪数组中的位置,另一个用于将当前元素与数组的所有其他元素进行比较。
您将错误的向量传递给binarySearch函数。您没有从selectionSort函数传递已排序的向量,而是传递了&#34;单词&#34;矢量而不是。这意味着即使selectionSort正在运行,您仍然无法得到正确的答案。
在您的binarySearch函数中,您的计数不起作用,因为&#34; i&#34;在while循环中。这意味着每次循环执行时,&#34; i&#34;和&#34;计数&#34;都重置为零。为了防止这种情况,请放置&#34; i&#34;在循环之外,这样它每次都可以实际增加。
我用四个元素列表测试了它,它似乎对我有用。如果您还有其他问题,请不要犹豫。用C ++再次进行编码很有趣。
另外,我不知道你的数字&#34;&#34;&#34;应该在这里工作。如果您将成功的比较计为一个比较,那么&#34;计数&#34;应该从一开始。如果成功比较没有计数,则计数从零开始。
void selectionSort(vector<string>& wordsSorted)
{
cout << "Perform selection sort... \n" << endl;
int min = 0;
string min1 = wordsSorted[0];
int lowestIndex = 0;
bool isBigger = false; // I used a boolean because I was kinda lazy. This checks if a swap is needed so a swap doesn't happen every run through.
for (int i = 0; i < wordsSorted.size(); i++)
{
min1 = wordsSorted[i];
for(int j = i + 1; j < wordsSorted.size(); j++) // Second for loop to find the smallest element
{
if (min1 > wordsSorted[j])
{
min1 = wordsSorted[j]; // Keeping track of the minimum word found so far
lowestIndex = j; // Keeping track of where the smallest element is in the array.
isBigger = true; // Noted that a swap is needed
}
}
if (isBigger) // If the boolean changed, then swap the words.
{
string temp = wordsSorted[i];
wordsSorted[i] = min1;
wordsSorted[lowestIndex] = temp;
isBigger = false;
}
}
}
int binarySearch(vector<string>& wordsSorted, string targetWord, int& count)
{
cout << "Perform a binary search using selection sorted vector... \n" << endl;
int first = 0,
last = wordsSorted.size() - 1,
mid,
position = -1,
i = 0;
bool found = false;
while (!found && first <= last)
{
//int i = 0;
count = i++;
mid = (first + last) / 2;
if (wordsSorted[mid] == targetWord) // If value is found at mid
{
found = true;
position = mid;
cout << "The target word was located successfully after performing " << count << " comparisons.";
return position;
}
else if (wordsSorted[mid] > targetWord) // Lower half
last = mid - 1;
else if (wordsSorted[mid] < targetWord) // Upper half
first = mid + 1;
else
cout << "After performing " << count << " comparisons, the target word could not be found.";
}
}