Heim  >  Artikel  >  Datenbank  >  大数据检索技巧之算法

大数据检索技巧之算法

WBOY
WBOYOriginal
2016-06-07 15:36:091142Durchsuche

/*拉朗日差查找算法*/ 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();
}

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Google搜索小技巧Nächster Artikel:??数据库