二分探索の再帰関数を作成します。
この関数は、ソートされた配列と検索する項目を受け入れ、項目のインデックス (項目が配列内にある場合) または -1 を返します (項目が配列内にない場合)。
さらに、関数をテストするテスト プログラムを作成します。
template <class elemType>
int orderedArrayListType<elemType>::binarysearch
(const elemType& item) const
{
int first= 0;
int last = length -1;
int mid;
int list[];
int BinarySearch(,Type & Item, int first, int last)
bool found = false;
while (first <= last && !found){
mid = (first + last) / 2;
if (list[mid] > item)
return BinarySearch(list, item, first, mid -1)
found = true;
else if (list[mid] > item)
return BinarySearch( list, item, first, mid -1)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
}