本文整理匯總了C++中BinarySearch函數的典型用法代碼示例。如果您正苦於以下問題:C++ BinarySearch函數的具體用法?C++ BinarySearch怎麽用?C++ BinarySearch使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了BinarySearch函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。
示例1: search
int search(vector<int>& nums, int target) {
int n = nums.size();
// if (n == 0) return -1;
// if (n == 1) return (target == nums[0])-1;
//find pivot
int pivot = -1;
int l = 0, r = n-1;
while (l < r)
{
int mid = l + (r-l)/2;
nums[mid] > nums[r]? l = mid+1: r = mid;
}
pivot = r-1; //index of maximum
cout <<pivot;
if (pivot == -1) //all ascending
return BinarySearch(nums, 0, n-1, target);
// if (target > nums[pivot] || target < nums[r] || (target >nums[n-1] && target < nums[0]))
// return -1;
else if (target >=nums[0])
return BinarySearch(nums, 0, pivot, target);
else
return BinarySearch(nums, pivot+1, n-1, target);
}
示例2: return
size_t CosineTree::BinarySearch(arma::vec& cDistribution,
double value,
size_t start,
size_t end)
{
size_t pivot = (start + end) / 2;
// If pivot is zero, first point is the sampled point.
if (!pivot)
{
return pivot;
}
// Binary search recursive algorithm.
if (value > cDistribution(pivot - 1) && value <= cDistribution(pivot))
{
return (pivot - 1);
}
else if (value < cDistribution(pivot - 1))
{
return BinarySearch(cDistribution, value, start, pivot - 1);
}
else
{
return BinarySearch(cDistribution, value, pivot + 1, end);
}
}
示例3: BinarySearch
int BinarySearch(int A[],int low,int high,int x){
if (low > high) return -1;
int mid = low + (high-low)/2; //low+high can overflow
if(x == A[mid]) return mid; // Found X, return (exit)
else if(x < A[mid]) return BinarySearch(A, low, mid-1,x); //X lies before mid
else return BinarySearch(A, mid+1, high, x); //X lies after mid
}
示例4: memset
//CDynamicArray &aWord: the words array
//CDynamicArray &aWordBinaryNet:the net between words
//double dSmoothingPara: the parameter of data smoothing
//CDictionary &DictBinary: the binary dictionary
//CDictionary &DictCore: the Core dictionary
bool CSegment::BiGraphGenerate(CDynamicArray &aWord, CDynamicArray &aBinaryWordNet,double dSmoothingPara,CDictionary &DictBinary,CDictionary &DictCore)
{
PARRAY_CHAIN pTail,pCur,pNextWords;//Temp buffer
unsigned int nWordIndex=0,nTwoWordsFreq=0,nCurWordIndex,nNextWordIndex;
//nWordIndex: the index number of current word
double dCurFreqency,dValue,dTemp;
char sTwoWords[WORD_MAXLENGTH];
m_nWordCount=aWord.GetTail(&pTail);//Get tail element and return the words count
if(m_npWordPosMapTable)
{//free buffer
delete [] m_npWordPosMapTable;
m_npWordPosMapTable=0;
}
if(m_nWordCount>0)//Word count is greater than 0
{
m_npWordPosMapTable=new int[m_nWordCount];//Record the position of possible words
memset(m_npWordPosMapTable,0,m_nWordCount*sizeof(int));
}
pCur=aWord.GetHead();
while(pCur!=NULL)//Set the position map of words
{
m_npWordPosMapTable[nWordIndex++]=pCur->row*MAX_SENTENCE_LEN+pCur->col;
pCur=pCur->next;
}
pCur=aWord.GetHead();
while(pCur!=NULL)//
{
if(pCur->nPOS>=0)//It's not an unknown words
dCurFreqency=pCur->value;
else//Unknown words
dCurFreqency=DictCore.GetFrequency(pCur->sWord,2);
aWord.GetElement(pCur->col,-1,pCur,&pNextWords);//Get next words which begin with pCur->col
while(pNextWords&&pNextWords->row==pCur->col)//Next words
{
//Current words frequency
strcpy(sTwoWords,pCur->sWord);
strcat(sTwoWords,WORD_SEGMENTER);
strcat(sTwoWords,pNextWords->sWord);
nTwoWordsFreq=DictBinary.GetFrequency(sTwoWords,3);
//Two linked Words frequency
dTemp=(double)1/MAX_FREQUENCE;
//Smoothing
dValue=-log(dSmoothingPara*(1+dCurFreqency)/(MAX_FREQUENCE+80000)+(1-dSmoothingPara)*((1-dTemp)*nTwoWordsFreq/(1+dCurFreqency)+dTemp));
//-log{a*P(Ci-1)+(1-a)P(Ci|Ci-1)} Note 0<a<1
if(pCur->nPOS<0)//Unknown words: P(Wi|Ci);while known words:1
dValue+=pCur->value;
//Get the position index of current word in the position map table
nCurWordIndex=BinarySearch(pCur->row*MAX_SENTENCE_LEN+pCur->col,m_npWordPosMapTable,m_nWordCount);
nNextWordIndex=BinarySearch(pNextWords->row*MAX_SENTENCE_LEN+pNextWords->col,m_npWordPosMapTable,m_nWordCount);
aBinaryWordNet.SetElement(nCurWordIndex,nNextWordIndex,dValue,pCur->nPOS);
pNextWords=pNextWords->next;//Get next word
}
pCur=pCur->next;
}
return true;
}
示例5: BinarySearch
int BinarySearch(int arr[],int first,int last,int target){
int mid;
if(first>last) return -1;
mid=(first+last)/2;
if(arr[mid]==target) return mid;
else if(arr[mid]>target) return BinarySearch(arr,first,mid-1,target);
else return BinarySearch(arr,mid+1,last,target);
}
示例6: _tmain
int _tmain(int argc, _TCHAR* argv[])
{
int i = BinarySearch(a, 101, 0, kSize);
i = BinarySearch(a, 76, 0, kSize);
i = BinarySearchIterative(a, 77, 0, kSize);
i = BinarySearchIterative(a, 3, 0, kSize);
i = BinarySearchIterative(a, 101, 0, kSize);
i = BinarySearchIterative(a, 76, 0, kSize);
return 0;
}
示例7: BinarySearch
// 在a的[low, high]區間內找到ley的位置並返回
int BinarySearch(int* a, int low, int high, int key){
if(low > high) return -1;
// 算出中間位置
int mid = (low + high) / 2;
if(key > a[mid]){
return BinarySearch(a, mid + 1, high, key);
} else if(key < a[mid]){
return BinarySearch(a, low, mid - 1, key);
} else return mid;
}
示例8: BinarySearch
void Table3D::InterpolatePoint_old(void) {
// Dimension 1
BinarySearch(interp->index[0], interp->weight[0][0], x1, 0, n1-1, point[0]);
interp->weight[0][1] = 1.0 -interp->weight[0][0];
// Dimension 2
BinarySearch(interp->index[1], interp->weight[1][0], x2, 0, n2-1, point[1]);
interp->weight[1][1] = 1.0 -interp->weight[1][0];
// Dimension 3
BinarySearch(interp->index[2], interp->weight[2][0], x3, 0, n3-1, point[2]);
interp->weight[2][1] = 1.0 -interp->weight[2][0];
}
示例9: BinarySearch
// Returns the index of number within array a or -1 if not found
int BinarySearch(const int a[], int number, int low, int high)
{
if (low < 0 || high < 0) return -1;
if (low > high) return -1;
int mid = (low + high) / 2;
if (number == a[mid]) return mid;
else if (number < a[mid])
return BinarySearch(a, number, low, mid - 1);
else // number > a[mid]
return BinarySearch(a, number, mid + 1, high);
}
示例10: BinarySearch
int BinarySearch(int* arr, int key, int min_idx, int max_idx) {
if(max_idx < min_idx) {
return -1; // Not Found
}
int mid_idx = min_idx + ((max_idx - min_idx) / 2);
if(key < arr[mid_idx]) {
return BinarySearch(arr, key, min_idx, mid_idx - 1);
}
if(key > arr[mid_idx]) {
return BinarySearch(arr, key, mid_idx + 1, max_idx);
}
return mid_idx;
}
示例11: main
int main(int argc, char** argv){
int i, j, k, m;
int A[] = {2,5,-3,45,12,4,8,19,3,12};
int B[] = {2,4,6,8,10,12,14,16,18,20};
i = SequentialSearch(A, 10, 12);
j = SequentialSearch(B, 10, 12);
k = BinarySearch(B, 10, 12);
m = BinarySearch(B, 10, 13);
printf("%d, %d, %d, %d\n", i, j, k, m);
return(EXIT_SUCCESS);
}
示例12: BinarySearch
/* Search an ordered array of strings in a recursive binary fashion.
* Returns the index of the string found. Less than 0 if false.
*/
int BinarySearch(char *name,int l, int h, char *array[])
{
register int m, rc;
if (l > h)
return (-1);
m = (l + h) / 2;
if ((rc = strcmp(name, array[m])) == 0)
return (m);
else if (rc < 0)
return (BinarySearch(name, l, m - 1, array));
else
return (BinarySearch(name, m + 1, h, array));
}
示例13: if
int BinarySearchType::BinarySearch(/* IN */ int First,
/* IN */ int Last)
//-------------------------------------------------
// Pre : First, Last and Value are assigned
// and 0 <= First, Last <= SIZE-1,
// where SIZE is the maximum size of the array,
// and pDC_[First..Last] and pValue_ are assigned
// Post: pDC_ has been search for Value point to by pValue_
// if Value is found
// returns index within DataContainer
// else
// returns -1
//-------------------------------------------------
{
int Index;
if (First > Last)
{
Index = -1; // Value not in original DataContainer
}
else
{
int Mid = (First + Last)/2;
//if (Value == A[Mid])
if ((*pValue_) == pDC_->GetItem(Mid))
{
Index = Mid; // Value found at DataContainer[Mid]
}
// else if (Value < A[Mid])
else if ((*pValue_) < pDC_->GetItem(Mid))
{
//BinarySearch(A, First, Mid-1, Value, Index); // X
Index = BinarySearch(First, Mid-1); // X
}
else
{
//BinarySearch(A, Mid+1, Last, Value, Index); // Y
Index = BinarySearch(Mid+1, Last); // Y
}
} // end else
return (Index);
} // end BinarySearch
示例14: free
void *multiGenerateSplit(void *arg)
{
void **argT=(void **)arg;
uint64_t num=(uint64_t )argT[0];
uint64_t THREAD_NUM=(uint64_t)argT[1];
free(argT);
uint64_t segment=BWTLEN/THREAD_NUM;
uint64_t start=num*segment,i;
uint64_t specialSApoint,branchPoint;
specialSApoint=BinarySearch(start,specialSA,countRead-1);
branchPoint=BinarySearch(start,specialBranch,specialBranchNum-1);
for(i=start;i<BWTLEN;i++)
{
unsigned int multioutFlag=0;
//check whether is multiout or not
uint64_t distance=specialSA[specialSApoint]-i;
if(distance>=KMER_LENGTH)// use the main module, for kmer info was stored in it
{
uint64_t checkSeq=convert(i)>>((32-KMER_LENGTH)<<1);
unsigned int formerSeq=checkSeq>>(REDLEN<<1);
uint64_t latterSeq=(checkSeq&REDEXTRACT);
uint64_t redLen;
//operation to check multiout, use hash
if(formerSeq>0)
{
redLen=blackTable[formerSeq]-blackTable[formerSeq-1];
}
else
{
redLen=blackTable[0];
}
if(redLen>0)
{
//binary search
uint64_t startPoint=blackTable[formerSeq]-redLen;
uint64_t *searchRedSeq=&redSeq[startPoint];
uint64_t candidate=BinarySearch_red(latterSeq,searchRedSeq,redLen-1);
if(candidate<redLen)
{
if((searchRedSeq[candidate]>>2)==latterSeq)
{
// Then we can judge whether it is multiout
unsigned int tempFlag=searchRedSeq[candidate]&3;
multioutFlag=tempFlag&1;
}
}
}
}
示例15: findSorted
void findSorted(int *list,int k,int n)
{
int i,j;
int x,y,IndexX=-1,IndexY=-1;
int low,high;
FILE *out=fopen("output.txt","w");
for(i=0; i<n ; i++)
{
x= list[i];
IndexX=i;
y=k-x;
IndexY=BinarySearch(list,y,n);
if(IndexY==-1)
printf("There is no x+y=k exist\n\n");
else if(list[IndexX] < list[IndexY])
{
printf("List[%d]'s element is %d\nList[%d]'s element is %d\n",IndexX,list[IndexX],IndexY,list[IndexY]);
printf("Sum of these is equal to %d\n\n",k);
fprintf(out,"%d %d \n",list[IndexY], list[IndexX]);
}
}
fclose(out);
}