亘古微博
咨询邮箱:nicholas@anycle.com
您的位置:网站首页 > 新闻动态 > 算法专题 >
[C] Binary sort
来源:未知 作者:admin 发表于:2015-03-11 17:18  点击
Just a simple example of binary sort:



#include <iostream>
#include "binary.h"
using namespace std;
 
#define ORG_DATA 1,4,5,6,7,8,10,16,19,105
#define LENGTH 10
#define SEARCH_ 8
 
void binary(){
      int data[LENGTH] = {ORG_DATA};
      int len = LENGTH;
      int search = SEARCH_;
 
      int begin = 0;
      int end = len-1;
      int idx = len/2;
      int ret = 0;
 
      cout << std::endl << "begin binary searching" << std::endl;
 
      for(int i=0; i< len; i++){
           cout << data[i] << "," ;
      }
 
      cout << std::endl << "search:" << search << std::endl;
 
      while(1){
           idx = (begin + end)/2;
           if(data[idx] > search){
                 end = idx;
           }else if(data[idx] < search){
                 begin = idx;
           }else{
                 ret = idx+1;
                 break;
           }
      }
 
      cout << std::endl << "result:" << ret << std::endl;
 
}
相关文章推荐
[C] Insertion sort

This is the insertion sort function by nicholas....

[C] Binary sort

Just a simple example of binary sort....

[C] Bubble sort

Bubble sort should be avoided in the case of large collections. It will not be ef...

[JS] How boys and girls stand balance

How boys and girls stand balance...