Application analysis of C++ binary search in search engine multi document intersection

  • 2020-04-02 03:06:50
  • OfStack

This paper illustrates the application of C++ binary search in search engine multi-document intersection. Share with you for your reference. The details are as follows:


int search2(int array[], int n, int v)
{
  int left, right, middle;
  left = 0, right = n - 1;
  while (left <= right)
  {
    middle = (left + right) / 2;
    if (array[middle] > v)
    {
      right = middle - 1;
    }
    else if (array[middle] < v)
    {
      left = middle + 1;
    }
    else
    {
      return middle;
    }
  }
  return -1;
}
int search3(int array[], int n, int v)
{
  int left, right, middle;
  left = 0, right = n;
  while (left < right)
  {
    middle = (left + right) / 2;
    if (array[middle] > v)
    {
      right = middle;
    }
    else if (array[middle] < v)
    {
      left = middle + 1;
    }
    else
    {
      return middle;
    }
  }
  return -1;
}

The algorithm complexity of binary search is log2n, which is an efficient search.

In the search, documents will be used to find the intersection, such as a user's retrieval, from each cluster online data, these documents may exist between the intersection, and the data provided is orderly, how to get intersection documents ?

This one can use binary search, in multiple ordered arrays of documents, pick the shortest one, and then pick one element at a time, in other arrays binary search, so you get the intersection document.

Hope that the article described in the C++ programming to help you.


Related articles: