鱼C论坛

 找回密码
 立即注册
查看: 2688|回复: 0

[学习笔记] 《数据结构与算法》——简单查找

[复制链接]
发表于 2017-8-22 16:19:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
二分查找法:将按大小顺序排列好的数字存放在数组,我们将low,high分别初始化为0和数组下标最大值,mid=(low+high)/2,不断的比较mid指向的元素和待查找元素key的值,若mid>key,则使high=mid,重复之前步骤;反正low=mid,直至key与mid指向元素相等,或low=high。
插值查找:原理与二分查找类似,但是插值查找利用比例来找到mid的下一个位置,即
mid=low+(key-a[low])/(a[high]-key)*(high-low),来确定下一个mid,进行递归或者迭代。
斐波那契查找:该算法将数组元素不断地分为两个斐波那契数,如大小为13的数组则被分为5和8两个部分,然后比较key与a[5],以此不断地进行递归或者迭代,直至low=high或找到目标key的下标。当数组元素个数不足时,用最大值补足


  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<time.h>
  4. #include<iostream>
  5. using namespace std;

  6. class LIST
  7. {
  8. public:
  9.         char name[10];
  10.         int score,ID;
  11. };

  12. int key;


  13. int Input(LIST *&s)
  14. {
  15.         cout << "input the numbers of students;";
  16.         int n;
  17.         cin >> n;
  18.         s = new LIST[n];
  19.         printf("input the student's name,ID numbers,score:\n");
  20.         for (int i = 0; i < n; i++)
  21.         {
  22.                 cin >> (s+i)->ID >> (s + i)->name >> (s + i)->score;
  23.         }
  24.         return n;
  25. }

  26. void Binary_search(LIST *s,int low,int high)
  27. {
  28.         int mid = (high + low) / 2;
  29.         int k = (s - mid)->ID;
  30.         if (low == high)
  31.         {
  32.                 cout << "no such student" << endl;
  33.                 return;
  34.         }
  35.         if (key < (s - mid)->ID)
  36.         {
  37.                 cout << (s - mid)->ID << endl;
  38.                 Binary_search(s, mid+1, high);
  39.         }
  40.         else if (key > (s - mid)->ID)
  41.         {
  42.                 cout << (s - mid)->ID << endl;
  43.                 Binary_search(s, low, mid);
  44.         }
  45.         else if (key == (s - mid)->ID)
  46.         {
  47.                 cout << "ID:" << (s + mid)->ID << "  name:" << (s + mid)->name << "  score:" << (s + mid)->score << endl;
  48.         }
  49. }

  50. void Inter_search(LIST *s, int low, int high)
  51. {
  52.         int mid = low + (key - (s + low)->ID) / ((s + high)->ID - (s + low)->ID + 1);
  53.         if (low == high)
  54.         {
  55.                 cout << "no such student" << endl;
  56.                 return;
  57.         }
  58.         if (key > (s + mid)->ID)
  59.         {
  60.                 Binary_search(s, mid, high);
  61.         }
  62.         else if (key < (s + mid)->ID)
  63.         {
  64.                 Binary_search(s, low, mid);
  65.         }
  66.         else if (key == (s + mid)->ID)
  67.         {
  68.                 cout << "ID:" << (s + mid)->ID << "  name:" << (s + mid)->name << "  score:" << (s + mid)->score << endl;
  69.         }
  70. }
  71. int Fabonacci(int n)
  72. {
  73.         int a = 1, b = 1;
  74.         int result = 1;
  75.         while (1)
  76.         {
  77.                 if (n > result&&n < a + b)
  78.                 {
  79.                         return a ;
  80.                 }
  81.                 else if(n>a+b)
  82.                 {
  83.                         result = a + b;
  84.                         b = a;
  85.                         a = result;
  86.                 }
  87.                 else if (n == a + b)
  88.                 {
  89.                         return result;
  90.                 }
  91.                 else if (n == result)
  92.                 {
  93.                         return b;
  94.                 }
  95.         }
  96. }
  97. void Fabonacci_search(LIST *s, int low,int high)
  98. {
  99.         int mid = Fabonacci(high-low)-1;
  100.         //判断是否有不存在的可能
  101.         if (key < (s + mid + low )->ID)
  102.         {
  103.                 Fabonacci_search(s, low, mid);
  104.         }
  105.         else if(key > (s + mid + low)->ID)
  106.         {
  107.                 Fabonacci_search(s, mid+1, high);
  108.         }
  109.         else if (key == (s + mid + low )->ID)
  110.         {
  111.                 cout << "ID:" << (s + mid )->ID << " name:" << (s + mid )->name << " score:" << (s + mid )->score << endl;
  112.         }
  113. }

  114. int main()
  115. {
  116.         LIST *s;
  117.         int n;
  118.         clock_t start,end;
  119.         n=Input(s);
  120.         int choice=1;
  121.         cout << "input the target ID number:";
  122.         cin >> key;
  123.         while (choice)
  124.         {
  125.                 printf("1——>二分查找\n2——>差值查找\n3——>斐波那契查找\n0——>结束程序\n");
  126.                 printf("input your choice:");
  127.                 scanf_s("%d", &choice);
  128.                 switch(choice)
  129.                 {
  130.                 case 1:
  131.                         start = clock();
  132.                         Binary_search(s,0,n);
  133.                         end = clock();
  134.                         cout << "used time is " << (double)(end - start)/ CLOCKS_PER_SEC << endl;
  135.                         break;
  136.                 case 2:
  137.                         start = clock();
  138.                         Inter_search(s,0,n);
  139.                         end = clock();
  140.                         cout << "used time is " << (double)(end - start) / CLOCKS_PER_SEC << endl;
  141.                         break;
  142.                 case 3:
  143.                         start = clock();
  144.                         Fabonacci_search(s,0,n);
  145.                         end = clock();
  146.                         cout << "used time is " << (double)(end - start) / CLOCKS_PER_SEC << endl;
  147.                         break;
  148.                 case 0:
  149.                         break;
  150.                 default:
  151.                         cout << "Wrong!"<<endl;
  152.                         break;

  153.                 }
  154.         }
  155.         system("PAUSE");
  156. }
复制代码

评分

参与人数 1鱼币 +2 收起 理由
小甲鱼 + 2

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 02:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表