在C ++中实现二进制搜索

时间:2015-11-27 19:11:18

标签: search binary

我对编程很新,我遇到了一个问题。我试图实现二进制搜索到100个随机整数的数组。我把它们按降序排列。但是,我不能让搜索工作,我相信我可能不完全理解它,(错过了课堂上的那一天,没有笔记,在互联网上工作)。任何帮助将不胜感激。我给了2个fcns。我正在调用它的二进制文件和fcn。抱歉,如果有很多代码。

int binary_search(int random[], int low, int high, int search)
{
  int index;

if (low > high)
    index = -1;

else
{
    int mid = (low + high) / 2;

    if (search == random[mid])
        index = mid;
    else

        if (search < random[mid])
            index = binary_search(random, low, mid - 1, search);
        else
            index = binary_search(random, mid + 1, high, search);



 } // end if
 return index;
  }// end binarySearch




int randomizer(int random[]){


srand((unsigned)time(NULL));

ofstream PRINT("P4Output.txt");
const int arraySize = 100; //size of the array
int high = 100; // high for random numbers
int low = -100; // low for random numbers
int size = 0;
int out = 0;
random[arraySize]; // giving the array random 100 elements
int first = 0;
int last = 0;
int index = 0;
int search = 0;




cout << endl << "                    RANDOM:                                       " << endl;
cout << " *************************************************" << endl << endl << endl;
PRINT << endl << "                   RANDOM:                                  " << endl;
PRINT << " ************************************************" << endl << endl << endl;

for (int i = 0; i < 100; i++){ // Start of loop to insert 100 random values          into array
random[i] = rand() % (high - low + 1) + low; //Inserting random values into array

    cout << setw(5) << random[i]; // outputs the random integer with spacing.
    PRINT << setw(5) << random[i];

    if ((i + 1) % 10 == 0) {

        cout << endl << endl << endl; // end of print line for set of 10 values
        PRINT << endl << endl << endl;
    }// end of if statement

} // End for-loop

cout << "                  SELECTION SORT:                                  " << endl;
cout << " *************************************************" << endl << endl << endl;
PRINT << "                 SELECTION SORT:                                  " << endl;
PRINT << " ************************************************" << endl << endl << endl;

selectionSort(random, arraySize); // calling selection sort

for (int j = 0; j < arraySize; j++){ // for-loop
    cout << setw(5) << random[j]; //outputs integers w/ spacing

    if ((j + 1) % 10 == 0){ // sets rows of ten
        cout << endl << endl;
    }
}// end of for-loop


cout << "                 BINARY SEARCH                    " << endl;
cout << " *************************************************" << endl << endl << endl;
PRINT << "                 BINARY SEARCH                    " << endl;
PRINT << " *************************************************" << endl << endl << endl;

 index = binary_search(random, first, last, search);

 for (int i = 0; i < arraySize; i++){
     while (search != 101){

         cout << "What number would you like to look for?" << endl;
         cin >> search;


         if (search == index)

             cout << "found @" << index;

         else
             cout << "not found" << endl;


     }

 }

return out;

}

2 个答案:

答案 0 :(得分:2)

如果您不想使用递归,请使用它..

ll Binary_search(ll val,ll n){
  ll lo = 1, hi = n;
     while(lo<=hi){
       ll mid = lo+(hi-lo)/2;
       if(arr[mid]==val)return mid;
       if(val>arr[mid])lo=mid+1;
       else if(val<arr[mid])hi=mid-1;   
     }
return -1; 
}
// array index start with 1 index

答案 1 :(得分:1)

为搜索功能执行一些简单的操作,返回找到的n index ,否则返回 -1 。像:

int binary_search(int A[], int l, int r, int n){
    int mid;
    if (l > r) return -1;
    while (l <= r){
        mid = (l + r) / 2;
        if (n == A[mid]){
            return mid;
        }
        else if (n < A[mid]){
            return binary_search(A, l, mid - 1, n);
        }
        else if (n > A[mid]){
            return binary_search(A, mid + 1, r, n);
        }
    }
    return -1;
}