BinarySearch功能不起作用

时间:2017-11-13 01:27:50

标签: c++ arrays function

我有一个我一直在做的菜单,除了二进制搜索功能外,现在一切似乎都有效。我输入数组的大小,填充数组,打印它,然后用我的时间顺序菜单排序,然后当我进行顺序搜索时,它似乎工作。但是,二进制搜索甚至不返回-1表示找不到该号码,程序就停止了。建议?感谢。

#include<iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int size=0; //global variable size
int a[100]; //global array

int getSize()
{
cout << "Array Size: ";
cin >> size;
return size;
cout << endl;
}

int sequentialSearch(int n)
{
for(int i=0;i<size;i++)
{
    if(n==a[i])
    {
        return (i+1);
}
}
return -1;
}

int binarySearch(int n)
{
int low=0,high=size,mid;
while(low<=high)
{
    mid=(low+high)/2;
    if(a[mid]==n)
    {

        return mid+1;
    }
    else if(a[mid]>n)
    high=mid-1;
else if(a[mid]<n)
    low=mid-1;
}
return -1;
}

 void sort1()
 {
int i, j;
for (i = 0; i < size-1; i++)     
{
   for (j = 0; j < size-i-1; j++)
       if (a[j] > a[j+1])
       {
           int temp=a[j];
           a[j]=a[j+1];
           a[j+1]=temp;
       }
}
}

void sort2()
{
int i, j, m;
for (i = 0; i < size-1; i++)
{
    m = i;
    for (j = i+1; j < size; j++)
    {
      if (a[j] > a[m])
        m = j;
    }
    int temp=a[m];
    a[m]=a[i];
    a[i]=temp;
    }
}


void printMenu()
{
cout<<"0. Exit\n";

cout<<"1.Get the size needed for todays use of the array \n";

cout<<"2.Fill an array with random numbers from 1-100 \n";

cout<<"3.Print the array with position numbers \n";

cout<<"4.Sort the array in ascending sequence \n";

cout<<"5.Sort the array in descending sequence \n";

cout<<"6.Sequential search of the array for a target\n";

cout<<"7.Binary search of the array for a target\n";
}

void printTheArray()
{
for(int i=0;i<size;i++)
{
    cout<<a[i]<<" is at position :"<<i+1<<endl;
}
}

void fillWithRandom()
{
for(int i=0;i<size;i++)
{
    int x=rand()%101;
a[i]=x;
}
}

void dispatch(int ch)
{
switch(ch)
{
    case 1:cout<<"Size of array :"<<getSize() << endl;
           break;
    case 2:fillWithRandom();
           break;
    case 3:    printTheArray();
           break;
    case 4:sort1();
           break;
    case 5:sort2();
           break;
    case 6:
        {
            cout<<"Enter the number you want to search\n";
                int t;
                cin>>t;
                int res1=sequentialSearch(t);
                if(res1!=-1)
                    cout<<"Found in position :"<<res1<<endl;
                else if(res1==-1)
                    cout<<"Not Found \n";
            break;
     }
    case 7:   
    {
        cout<<"Enter the number you want to search\n";
            int t;
            cin>>t;
            int res=binarySearch(t);
            if(res!=-1)
                cout<<"Found in position :"<<res<<endl;
            else if(res==-1)
                cout<<"Not Found \n";
            break;
    }
            default:cout<<"wrong choice\n";
}
}

int main ()
{

printMenu();
int choice;
cout<<"Type in a choice"<<endl;
cin>>choice;

while(choice!=0)
{
    dispatch(choice); // one big switch statement       
    printMenu();
    cout<<"Type in a choice"<<endl;
    cin>>choice;
}

cout << endl;


// wrap-up

cout << "This program is coded by Troy Wilms" << endl;  // fill in your name

// stops the program to view the output until you type any character
return 0;
}

1 个答案:

答案 0 :(得分:1)

例如,如果你尝试在列表{1,3,5}中找到4,则低位将始终为0,高位将始终为2.因为代码“低”=“中” - 1&#39;我认为它应该改为&#39; low = mid + 1&#39;