带有字符串数组的二叉搜索算法
Binary search algorithm with string array
程序应该在字符串数组中搜索名称。最初,该程序适用于 int 数据类型,但更改为字符串。我试图修改代码以使其运行,它确实如此,但我没有得到正确的输出。我的二进制搜索功能有问题。我的二叉搜索函数尚未正确编辑以适应数据类型从 int 更改为字符串的变化。它不起作用,因为我在数组中有字符串而不是最初为其创建的 int。其他一切似乎都正常工作。
这是我得到的输出。
请输入员工姓名:卢尼
该名称确实存在于数组中。
从技术上讲,我应该得到它存在的信息以及它的立场。
// This program demonstrates the binarySearch function, which
// performs a binary search on an integer array.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
// Function prototype
void sortArray(string [], int);
int binarySearch(string [], int, string);
const int SIZE = 20;
int main()
{
// Defined array
const int NUM_NAMES = 20;
string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
"Griffin, Jim", "Stamey, Marty", "Rose, Geri",
"Taylor, Terri", "Johnson, Jill", "Allison, Jeff",
"Looney, Joe", "Wolfe, Bill", "James, Jean",
"Weaver, Jim", "Pore, Bob", "Rutherford, Greg",
"Javens, Renee", "Harrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth" };
// Variables
string empName;
int results;
// Sort array first
sortArray(names, NUM_NAMES);
// Prompt for user input to enter an employee name
cout << "Please enter an employee's name: ";
getline(cin, empName);
// Search for name
results = binarySearch(names, NUM_NAMES, empName);
// If results contains -1 the name was not found.
if (results == -1)
cout << "That name does not exist in the array.n";
else
{
// Otherwise results contains the subscript of
// the specified employee ID in the array.
cout << "That name is found at element " << results;
cout << " in the array.n";
}
system("PAUSE");
return 0;
}
//**************************************************************
// Definition of function sortArray. *
// This function performs an ascending order selection sort on *
// array. size is the number of elements in the array. *
//**************************************************************
void sortArray(string names[], int size)
{
int startScan, minIndex;
string minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = names[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (names[index] < minValue)
{
minValue = names[index];
minIndex = index;
}
}
names[minIndex] = names[startScan];
names[startScan] = minValue;
}
}
//***************************************************************
// The binarySearch function performs a binary search on an *
// integer array. array, which has a maximum of size elements, *
// is searched for the number stored in value. If the number is *
// found, its array subscript is returned. Otherwise, -1 is *
// returned indicating the value was not in the array. *
//***************************************************************
int binarySearch(string names[], int size, string value)
{
int first = 0, // First array element
last = size - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (names[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (names[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
}
因为数组中不存在名称Looney
。
您应该输入Looney, Joe
以获得预期的输出。
输入是错误的,否则您的实现看起来很好。
相关文章:
- 在 C++ 中搜索 2D 数组以查看一行/列是否与另一行/列相同
- 使用指针算法搜索 2D 数组
- 矢量搜索与数组搜索的时间成本
- 搜索字符数组
- 搜索字符数组中的子字符串
- 如何在C++中搜索字符串数组
- 难以搜索"craigslist"数组以查找匹配项并删除匹配的行并将所有内容向上移动
- 搜索 2D 数组并返回整行
- 如何在 C# 中优雅地搜索结构数组,从 C++ 移植
- 使用双截搜索从数组中删除字符串
- 对排序和搜索的数组没有输出
- 搜索字符串数组
- 获取用户输入并搜索2D数组
- C++搜索动态数组
- 使用指针和线性搜索在数组中找到较大和最小的数字
- 如何在c++中搜索字符串数组
- 搜索并行数组
- c++搜索2d数组
- 线性搜索返回数组,索引值在
- 搜索字符串数组