C++中的二叉搜索
Binary search in C++
基本上,我需要找出如何在屏幕上输出值,以便我的二进制搜索成功部分。我试图更改last
的初始值,但它要么崩溃,要么保持不变。 我逐步浏览了代码,一切似乎都正常工作。
编译器中的程序示例
#include<iostream>
using namespace std;
void binarySearch();
int main()
{
//Called the function in main
binarySearch();
system("pause");
return 0;
};
//void function for binary Search
void binarySearch()
{
//creating array
int array[100];
array[0] = 1;
int i,target;
// using Boolean to create pattern for generated numbers in the array
bool check = false;
//loop to implement pattern
for (int x = 1; x < 100; x++)
{
if (check == true)
{
array[x] = array[x - 1] + 1;
check = false;
}
else
{
array[x] = array[x - 1] + 2;
check = true;
}
}
**Code found online and modified to fit**
int first,mid,last,completed,successful,tests;
completed = 0;
successful = 0;
tests = 0;
double percentage;
percentage = 1;
for(int x=0;x<100;x++)
{
// Initialize first and last variables.
first = 0;
last = 2;
srand( (unsigned)time( NULL ) );
int target = (rand() % 150) + 1;
while(first <= last)
{
mid = (first + last)/2;
if(target > array[mid])
{
first = mid + 1;
tests++;
}
else if(target < array[mid])
{
last = mid + 1;
tests++;
}
else
{
first = last - 1;
}
if(target == array[mid])
{
successful++;
}
}
completed++;
}
**Area which the error occur No value for successful**
//Output on screen
cout << endl;
cout << "There were "<< completed <<" searches completed."<< endl;
cout << "There were "<< successful <<" successful searches." << endl;
cout << percentage <<"%"<<" of the searches were successful." << endl;
cout << "There was an average of " << completed << " tests per search." << endl;
cout << endl;
}
我想
我会从将代码分解成更小的部分开始,每个部分我至少希望理解;我不够聪明,无法确定我理解像你的binarySearch
一样大的"块"。
它看起来像这样:
int array[100];
array[0] = 1;
int i,target;
// using Boolean to create pattern for generated numbers in the array
bool check = false;
//loop to implement pattern
for (int x = 1; x < 100; x++)
{
if (check == true)
{
array[x] = array[x - 1] + 1;
check = false;
}
else
{
array[x] = array[x - 1] + 2;
check = true;
}
}
应该初始化一个数组,所以我把它放在一个单独的函数中,然后稍微简化一下。首先进入一个函数:
int init(int array[100]) {
array[0] = 1;
int i,target;
// using Boolean to create pattern for generated numbers in the array
bool check = false;
//loop to implement pattern
for (int x = 1; x < 100; x++)
{
if (check == true)
{
array[x] = array[x - 1] + 1;
check = false;
}
else
{
array[x] = array[x - 1] + 2;
check = true;
}
}
}
然后简化:
int init(int *array) {
array[0] = 1;
for (int i=1; i<100; i++)
array[i] = array[i-1] + 1 + (i&1);
}
然后我会对binarySearch
的二叉搜索部分做大致相同的操作:
while(first <= last)
{
mid = (first + last)/2;
if(target > array[mid])
{
first = mid + 1;
tests++;
}
else if(target < array[mid])
{
last = mid + 1;
tests++;
}
else
{
first = last - 1;
}
if(target == array[mid])
{
successful++;
}
并简化:
int *binary_search(int const *left, int const *right, int *tests, int val) {
int const *mid = left + (right - left)/2;
while (left < right) {
++*tests;
if (val < *mid)
right = mid;
else
left = mid + 1;
}
return left;
}
最后,您需要代码来生成一个随机数,搜索它,跟踪有关搜索次数和成功次数的统计信息等。
您有几个问题。
首先,正如我评论的那样,您没有正确初始化last
。
其次,在else if(target < array[mid])
情况下,您没有正确修改last
。您正在设置last = mid + 1;
但您需要改为设置last = mid - 1;
接下来,您没有正确退出循环。只有在第一个>最后一个时退出 while 循环,但您应该退出一次target == array[mid]
;也许战略break;
声明会有所帮助。
当我进行这三个更改时,应用程序每次都会运行直到完成,但是我会收到 0 或 100 次成功的搜索 - 两者之间没有任何内容。
不过,这应该足以让您走得更远。
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 在C++中搜索嵌套多映射值
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 为什么二进制搜索在我的测试中不起作用
- 正在尝试重载二进制搜索树分配运算符
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 向量上的线性搜索
- 如何在动态数组上使用搜索函数
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- cmake:添加要搜索头文件的目录
- 使用C++创建特殊的二叉搜索树
- 在C++的字符串中搜索和删除某些字符
- std::unordered_map 搜索算法是如何实现的?
- 使用不变量来确定二分搜索中的边界条件
- 二叉搜索如何比线性搜索更快?
- 按边长度递归搜索图中所有可行路径
- QStackWidget - 按名称搜索
- 在递归二叉搜索树中搜索
- 我的二进制搜索程序只是关闭了