C++ 二进制搜索函数不打印
C++ Binary Search function does not print
谁能帮我修正这段代码? main 确实填充了从 1 到 1000 的 1000 个随机数的数组,当它进入函数时它按升序排序并遵循二进制搜索,但问题是它没有打印出任何东西。
int f10(int array[], int size)
{
int temp; // temporary swap variable
cout << "Ascending Order:" << endl;
for (int x = 1; x <= size; x++)
{ //how times
for (int j = 1; j <= size; j++)
{
if (array[j] > array[j + 1])
{
//we need to swap
temp = array[j]; //temp is holding the first value
array[j] = array[j + 1]; //
array[j + 1] = temp; //temp holds the second value
}
}
}
for (int x = 1; x <= size; x++)
{
cout << array[x] << ", ";
}
int value;
cout << "\nGimme a number to search:\n\t";
cin >> value;
int left = array[1], right = array[1000];
while (left < right)
{
int middle = (left + right) / 2;
if (array[middle] == value)
{
return middle;
}
else if (array[middle] > value)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return -1;
}
您的代码中几乎没有错误。检查这个。只为 10 个数字做这件事。通过编写随机数逻辑使其达到 1000 或任何您喜欢的值。
#include <iostream>
int f10(int arr[],int size);
using namespace std;
int main()
{
int arr[10] = {22,53,14,11,75,6,7,1,8,88};
int result_index;
result_index = f10(arr,10);
cout << "Result = "<<result_index<<endl;
}
int f10(int array[], int size) {
int temp; // temporary swap variable
cout << "Default Array:" << endl;
for (int x=0; x < size; x++)
{
cout<< array[x]<<", ";
}
cout << "Ascending Order:" << endl;
for (int x = 0; x < size; x++){
for(int j = 0; j < size; j++) {
if(j != size-1 && array[j] > array[j+1]) { // Major Change
// Swapping
temp = array[j]; //temp is holding the first value
array[j] = array[j+1]; //
array[j+1] = temp; //temp holds the second value
}
}
}
for (int x=0; x < size; x++)
{
cout<< array[x]<<", ";
}
int value;
cout <<"\nGimme a number to search:\n\t";
cin >> value;
int left = 0, right = 9; // Major change
while (left <= right){ // Major change
int middle = (left + right) / 2;
if (array[middle] == value)
{
return middle;
}
else if (array[middle] > value)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return -1;
}
谁能帮我修正这段代码? main 确实填充了从 1 到 1000 的 1000 个随机数的数组,当它进入函数时它按升序排序并遵循二进制搜索,但问题是它没有打印出任何东西。
int f10(int array[], int size)
{
int temp; // temporary swap variable
cout << "Ascending Order:" << endl;
for (int x = 1; x <= size; x++)
{ //how times
for (int j = 1; j <= size; j++)
{
if (array[j] > array[j + 1])
{
//we need to swap
temp = array[j]; //temp is holding the first value
array[j] = array[j + 1]; //
array[j + 1] = temp; //temp holds the second value
}
}
}
for (int x = 1; x <= size; x++)
{
cout << array[x] << ", ";
}
int value;
cout << "\nGimme a number to search:\n\t";
cin >> value;
int left = array[1], right = array[1000];
while (left < right)
{
int middle = (left + right) / 2;
if (array[middle] == value)
{
return middle;
}
else if (array[middle] > value)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return -1;
}
您的代码中几乎没有错误。检查这个。只为 10 个数字做这件事。通过编写随机数逻辑使其达到 1000 或任何您喜欢的值。
#include <iostream>
int f10(int arr[],int size);
using namespace std;
int main()
{
int arr[10] = {22,53,14,11,75,6,7,1,8,88};
int result_index;
result_index = f10(arr,10);
cout << "Result = "<<result_index<<endl;
}
int f10(int array[], int size) {
int temp; // temporary swap variable
cout << "Default Array:" << endl;
for (int x=0; x < size; x++)
{
cout<< array[x]<<", ";
}
cout << "Ascending Order:" << endl;
for (int x = 0; x < size; x++){
for(int j = 0; j < size; j++) {
if(j != size-1 && array[j] > array[j+1]) { // Major Change
// Swapping
temp = array[j]; //temp is holding the first value
array[j] = array[j+1]; //
array[j+1] = temp; //temp holds the second value
}
}
}
for (int x=0; x < size; x++)
{
cout<< array[x]<<", ";
}
int value;
cout <<"\nGimme a number to search:\n\t";
cin >> value;
int left = 0, right = 9; // Major change
while (left <= right){ // Major change
int middle = (left + right) / 2;
if (array[middle] == value)
{
return middle;
}
else if (array[middle] > value)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return -1;
}