当前位置: 首页>>代码示例>>C++>>正文


C++ BinarySearch函数代码示例

本文整理汇总了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);
 
 }
开发者ID:wxn7,项目名称:Leetcode,代码行数:25,代码来源:33.+SearchinRotatedArray.cpp

示例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);
  }
}
开发者ID:AmesianX,项目名称:mlpack,代码行数:27,代码来源:cosine_tree.cpp

示例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
}
开发者ID:sutariadeep,项目名称:datastructures_C,代码行数:7,代码来源:binary_search_recursive.c

示例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;
}
开发者ID:firememory,项目名称:dfwbi,代码行数:63,代码来源:Segment.cpp

示例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);
}
开发者ID:Trietptm-on-Coding-Algorithms,项目名称:TwoRivers,代码行数:9,代码来源:BinarySearch.c

示例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;
}
开发者ID:attilaszaboo,项目名称:Exercises,代码行数:11,代码来源:BinarySearch.cpp

示例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;
}
开发者ID:vell001,项目名称:vblog_octopress,代码行数:12,代码来源:binary_search.c

示例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];
}
开发者ID:ale11,项目名称:JoeX,代码行数:11,代码来源:Table3D.cpp

示例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);
}
开发者ID:attilaszaboo,项目名称:Exercises,代码行数:13,代码来源:BinarySearch.cpp

示例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;
}
开发者ID:inazo1115,项目名称:snippet,代码行数:13,代码来源:binary_search.cpp

示例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);
}
开发者ID:rubensamf,项目名称:CMPS12B,代码行数:14,代码来源:Search.c

示例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));
}
开发者ID:gmischler,项目名称:Dxf2rad-Radout,代码行数:18,代码来源:utils.c

示例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
开发者ID:clmitchell289,项目名称:CSUMB-CST238,代码行数:48,代码来源:BinarySearchType.cpp

示例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;
					}
				}
			}
		}
开发者ID:hitbc,项目名称:deBWT,代码行数:48,代码来源:generateSP.c

示例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);
     
          

}
开发者ID:ugurdonmez,项目名称:tutorials,代码行数:34,代码来源:sorted.cpp


注:本文中的BinarySearch函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。