二分搜索

template <typename T>
int binSearch(const T arr[], int first, int last, const T& target)
{
 int mid;                     // index of the midpoint
 T midValue;             // object that is assigned arr[mid]
 int origLast = last;  // save original value of last
 
 while (first < last)  // test for nonempty sublist
 {
  mid = (first+last)/2;
  midValue = arr[mid];
  if (target == midValue)
      return mid;                                        // have a match
      // determine which sublist to search
  else if (target < midValue)
       last = mid;   // search lower sublist. reset last
  else
       first = mid+1;  // search upper sublist. reset first
 }

 return origLast;   // target not found
}

你可能感兴趣的:(object,search)