binarysearch.cpp

来自「自己编的小程序,关于binarysearch」· C++ 代码 · 共 43 行

CPP
43
字号
//************      BinarySearch.cpp      *************


#include "BinarySearch.h"

Binarysearch::Binarysearch( int *xbptr,int xsearchKey,int xlow,int xhigh,int xsize )
:bptr(xbptr),searchKey(xsearchKey),low(xlow),high(xhigh),size(xsize)
{
	bptr = new int [xsize];
	for ( int i = 0; i < xsize; i++ )
		bptr[i] = xbptr[i];
}

//int Binarysearch::search( int *xxbptr,int xxsearchKey,int xxlow,int xxhigh,int xxsize )
int Binarysearch::search()
{
	int middle;
	//bptr = xxbptr;
	//for ( int i = 0; i < xxsize; i++ )
		//b[i] = xxb[i];
	//searchKey=xxsearchKey;
	//low=xxlow;
	//high=xxhigh;
	//size=xxsize;
	while ( low <= high )
	{
		middle = (low + high) / 2 ;
		if ( searchKey == bptr[middle] )
			return middle;
		else
			if( searchKey < bptr[middle] )
				high = middle - 1;
			else
				low = middle + 1;
	}
	return -1;
}


Binarysearch::~Binarysearch()
{
	delete[] bptr;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?