【问题标题】:Implement binary search with string array instead of int array使用字符串数组而不是 int 数组实现二进制搜索
【发布时间】:2015-04-14 17:31:05
【问题描述】:

该程序应该将其合并到其中。我创建了大部分代码,但这是我实验室的第二部分,我必须返回并输入字符串以进行二进制搜索。如果我必须删除 idNum 和结果以及 empId,我不知道如何搜索字符串。

    const int NUM_NAMES = 20;
       string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
                                   "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
                                   "Taylor, Terri", "Johnson, Jill", "Allison, Jeff",
                                   "Looney, Joe", "Wolfe, Bill", "James, Jean",
                                   "Weaver, Jim", "Pore, Bob", "Rutherford, Greg",
                                   "Javens, Renee", "Harrison, Rose", "Setzer, Cathy",
                                   "Pike, Gordon", "Holland, Beth" };

因此,二进制搜索将使用字符串而不是 int。我不明白该怎么做。

    // Function prototype
    int binarySearch(const int [], int, int);
    const int SIZE = 20;

    int main()
    {

        // Array with employee IDs sorted in ascending order.
       int idNums[SIZE] = {101, 142, 147, 189, 199, 207, 222,
                           234, 289, 296, 310, 319, 388, 394,
                           417, 429, 447, 521, 536, 600};
       int results;   // To hold the search results
       int empID;     // To hold an employee ID

       // Get an employee ID to search for.
       cout << "Enter the employee ID you wish to search for: ";
       cin >> empID;

       // Search for the ID.
       results = binarySearch(idNums, SIZE, empID);

       // If results contains -1 the ID was not found.
       if (results == -1)
          cout << "That number does not exist in the array.\n";
       else
       {
          // Otherwise results contains the subscript of
          // the specified employee ID in the array.
          cout << "That ID is found at element " << results;
          cout << " in the array.\n";
       }
       return 0;
    }

binarySearch 函数对整数数组执行二进制搜索。搜索具有最大 size 个元素的数组以查找存储在 value 中的数字。如果找到数字,则返回其数组下标。否则返回-1,表示该值不在数组中。

int binarySearch(const int array[], int size, int value)
{
   int first = 0,             // First array element
       last = size - 1,       // Last array element
       middle,                // Mid point of search
       position = -1;         // Position of search value
   bool found = false;        // Flag

   while (!found && first <= last)
   {
      middle = (first + last) / 2;     // Calculate mid point
      if (array[middle] == value)      // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (array[middle] > value)  // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;           // If value is in upper half
   }
   return position;
}

【问题讨论】:

    标签: c++ arrays sorting c++11 binary-search


    【解决方案1】:

    改变

    int binarySearch(const int array[], int size, int value)
    

    int binarySearch(const std::string array[], int size, const std::string &value)
    

    然后按如下方式使用

    std::string name;
    std::getline(std::cin, name); // read name from console
    std::sort(names, names + NUM_NAMES); // make sure the array is sorted
    results = binarySearch(names, NUM_NAMES, name);
    

    你也可以使用模板,让你的代码更通用

    template<typename T>
    int binarySearch(const T array[], int size, const T &value)
    

    【讨论】:

    • 在排序功能中,我为什么要做“names + NUM_NAMES”?我将如何在原型中展示这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 2023-03-24
    • 2022-10-02
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    相关资源
    最近更新 更多