-1

I am currently trying to complete a project. This code is just a binary search algorithm that i have been using. This is my first search algorithm i have ever done, as i am a self learning programmer i need some advice on how to get this too work. I am using QT creator as an IDE.

I do not get any errors in my code. However, when i run the code the answer is always "No Match Found". Im thinking it has something to do with the array itself as i tested it with 10 integers before and worked perfectly fine.

I know im not very good at explaining this as my english is poor. But if you have any questions please get back to me.

Code Written in C++

#include <iostream>
  using namespace std;

float binarySearch(float arr[], int left, int right, float x)
{
  while (left <= right)
  {
    int mid = left + (right - left) / 2;

    if (arr[mid] == x)
    {
      return mid;
    }
    else if (arr[mid] < x)
    {
      left = mid + 1;
    }
    else
    {
      right = mid - 1;
    }
  }

  return -1;
}

int main()
{
  float num;
  float output;

  float myarr[100] = {44.64978683, 44.62352548, 44.5972165, 44.57086043, 44.54445783,
                    44.51800925, 44.49151522, 44.46497629, 44.43839299, 44.41176587,
                    44.38509546, 44.35838228, 44.33162687, 44.30482976, 44.27799146,
                    44.25111251, 44.22419342, 44.1972347, 44.17023688, 44.14320046,
                    44.11612596, 44.08901388, 44.06186473, 44.03467901, 44.00745722,
                    43.98019985, 43.95290742, 43.9255804, 43.89821929, 43.87082457,
                    43.84339674, 43.81593628, 43.78844366, 43.76091937, 43.73336389,
                    43.70577768, 43.67816122, 43.65051497, 43.62283941, 43.595135,
                    43.56740221, 43.53964148, 43.51185328, 43.48403806, 43.45619628,
                    43.42832838, 43.40043481, 43.37251603, 43.34457246, 43.31660456,
                    43.28861275, 43.26059748, 43.23255917, 43.20449827, 43.17641519,
                    43.14831036, 43.12018421, 43.09203716, 43.06386963, 43.03568203,
                    43.00747477, 42.97924828, 42.95100295, 42.92273919, 42.89445742,
                    42.86615803, 42.83784141, 42.80950798, 42.78115811, 42.75279222,
                    42.72441067, 42.69601387, 42.6676022, 42.63917604, 42.61073577,
                    42.58228177, 42.55381441, 42.52533407, 42.49684112, 42.46833593,
                    42.43981886, 42.41129027, 42.38275054, 42.35420001, 42.32563904,
                    42.29706799, 42.26848721, 42.23989705, 42.21129785, 42.18268996,
                    42.15407372, 42.12544947, 42.09681755, 42.06817829, 42.03953203,
                    42.01087909, 41.98221981, 41.9535545, 41.9248835, 41.89620711};

  while(true)
  {
      cout << "Please enter an element to search" << endl;
      cin >> num;

      output = binarySearch(myarr, 0, 100, num);

      if (output == -1)
      {
        cout << "No Match Found" << endl;
      }
      else
      {
        cout << "Match found at position: " << output << endl;
      }
  }
}
  • 1
    Try to use `int` first and make it work with `int`s. Then you can switch to `float`. `float` have some subtleties, because they cannot represent the numbers you type, so `==` won't work unless your design your program around float representation correctness. – spectras Jan 13 '21 at 10:58
  • Your `binarySearch` assumes ascending order and your array has descending order. Also read this https://stackoverflow.com/q/588004/1387438 – Marek R Jan 13 '21 at 12:13

1 Answers1

3

The elements of your array myarr is sorted in decending order, so left should be set to mid+1 when current element is larger than target, not smaller.

Wrong:

    else if (arr[mid] < x)

Correct:

    else if (arr[mid] > x)

Also the array has only 100 elements, so the initial right should be 99, not 100.

Wrong:

      output = binarySearch(myarr, 0, 100, num);

Correct:

      output = binarySearch(myarr, 0, 99, num);
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • @Damien No recursivity is required for binary search. [The fixed code will work](https://wandbox.org/permlink/G12xT6IDc1vhOkb5). Why do you think it should exist? – MikeCAT Jan 13 '21 at 11:10