Home  >  Article  >  Database  >  大数据检索技巧之算法

大数据检索技巧之算法

WBOY
WBOYOriginal
2016-06-07 15:36:091193browse

/*拉朗日差查找算法*/ int search2x(double *p, int n, double key) { int tou, wei, zhong; tou = 0;//数组从0开始 wei = n - 1;//数组的尾部 int ci = 0;//统计次数 while (tou = wei) //头尾重合终止循环, { ci; //1/2换成插比例,一次砍一大半 zhong =

/*拉格朗日差值查找算法*/

int  search2x(double *p, int n, double key)
{
 int tou, wei, zhong;
 tou = 0;//数组从0开始
 wei = n - 1;//数组的尾部
 int ci = 0;//统计次数
 while (tou  {
  ci++;

  //1/2换成插值比例,一次砍一大半
  zhong = tou + (wei - tou) * (key -p[tou]) / (p[wei]-p[tou]);//算法关键点

  if (key == p[zhong])
  {
   printf("\n循环%d次找到", ci);
   return zhong;//找到
  }
  else if (key   {
   wei = zhong - 1;//砍掉一半
  }
  else
  {
   tou = zhong + 1;//砍掉一半
  }

 }
 printf("\n循环%d次", ci);
 return -1;

}

void main()
{

 double db[100000];//十万个数据
 for (int i = 0; i  {
  db[i] = i + 0.1;//有序
 }

 int find = search2x(db, 100000, 79876.1);
 if (find == -1)
 {
  printf("没有找到");
 }
 else
 {
  printf("find  %f", db[find]);//找到数据
 }

 getchar();
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Google搜索小技巧Next article:??数据库