?? p231.cpp
字號:
#include "iostream.h"
#include "p228.cpp"
template <class Type> class OrderedList : public DataList<Type> {
public:
OrderedList ( int sz = 10 ) : DataList<Type> (sz) { }
virtual ~OrderedList ( ) { }
virtual int BinarySearch ( const Type & x, const int low, const int high ) const;
virtual int BinarySearch ( const Type & x ) const ;
};
template <class Type>
int OrderedList<Type>::BinarySearch ( const Type & x, const int low, const int high ) const {
int mid = -1;
if ( low <= high ) {
mid = ( low + high ) / 2;
if ( Element[mid].getKey( ) < x ) //中點(diǎn)的關(guān)鍵碼小于給定值,右縮搜索區(qū)間
mid = BinarySearch ( x, mid+1, high );
else if ( Element[mid].getKey( ) > x ) //中點(diǎn)的關(guān)鍵碼大于給定值,左縮搜索區(qū)間
mid = BinarySearch ( x, low, mid-1 );
}
return mid;
}
template <class Type> int OrderedList<Type>::BinarySearch ( const Type & x ) const {
int high = CurrentSize, low = 1, mid;
while ( low <= high ) {
mid = ( low + high ) / 2;
if ( Element[mid].getKey ( ) < x ) low = mid + 1; //右縮搜索區(qū)間
else if ( Element[mid].getKey ( ) > x ) high = mid - 1; //左縮搜索區(qū)間
else return mid; //搜索成功,返回找到位置
}
return -1; //搜索失敗,返回-1
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -