当前位置: 首页 > news >正文

查找学习笔记

1、静态查找表

以下查找的索引均从1开始

(1)顺序查找(带哨兵)

#include<iostream>
#include<vector>using namespace std;int search(vector<int> arr, int key) {arr[0] = key;int i;for (i = arr.size() - 1; arr[i] != key; i--);return i;
}int main()
{int n;cin >> n;vector<int> list(n+1);for (auto i = list.begin() + 1; i != list.end(); i++) {cin >> *i;}int t;cin >> t;while (t--) {int key;cin >> key;int check = search(list, key);if (check != 0) {cout << check << endl;}else {cout << "error" << endl;}}return 0;
}

(2)折半查找(二分查找)

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;int search(vector<int> arr, int key) {int beg = 1, end = arr.size() - 1;int mid;while (beg <= end) {mid = (end + beg) / 2 ;if (arr[mid] == key) break;else if (arr[mid] < key) {beg = mid + 1;}else end = mid - 1;}if (arr[mid] == key)return mid;else return 0;
}int main()
{int n;cin >> n;vector<int> list(n+1);for (auto i = list.begin() + 1; i != list.end(); i++) {cin >> *i;}int t;cin >> t;while (t--) {int key;cin >> key;int check = search(list, key);if (check != 0) {cout << check << endl;}else {cout << "error" << endl;}}return 0;
}

扩展:次优查找树

考虑每个元素被查询的概率不同,概率高的元素应尽早被查询

转载:查找算法 | 静态树表(次优查找树)详细分析-CSDN博客

(3)索引顺序查找(分块查找)

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;struct Ind {int maxnum;int start;
};int search(vector<int> list, vector<Ind> ind, int key, int &time){ int i;for (i = 0; i != ind.size(); i++) {time++;if (key <= ind[i].maxnum) break;}if (i == ind.size()) return -1;int j = ind[i].start;for (; j <= list.size() - 1 && list[j] <= ind[i].maxnum; j++) {time++;if (list[j] == key)break;}if (j > list.size() - 1 || list[j] != key) return -1;else return j;
}int main()
{int n;cin >> n;vector<int> list(n + 1);for (auto i = list.begin() + 1; i != list.end(); i++) {cin >> *i;}// 构建索引表int m;cin >>  m;vector<Ind> ind(m);int tmp = 1;for (auto& i : ind) {cin >> i.maxnum;i.start = tmp;tmp += n / m;}int t;cin >> t;while (t--) {int key;cin >> key;int time = 0; // time记录查询次数int check = search(list, ind, key, time);if (check == -1) {cout << "error" << endl;}else {cout << check << "-" << time << endl;}}return 0;
}

应用:美团外卖订单中,按同一时刻来对订单分块,但同一时刻中的订单间是无序的。

http://www.lryc.cn/news/242508.html

相关文章:

  • Qt QIODevice介绍
  • python -opencv 中值滤波 ,均值滤波,高斯滤波实战
  • 【教学类-06-07】20231124 (55格版)X-X之间的加法、减法、加减混合题
  • postgresql经常出现连接一会后服务器拒绝连接
  • 迈巴赫S480升级主动式氛围灯 浪漫婉转的气氛
  • Leetcode103 二叉树的锯齿形层序遍历
  • 可观测性建设实践之 - 日志分析的权衡取舍
  • Ceres使用
  • 深度学习第1天:深度学习入门-Keras与典型神经网络结构
  • 青云科技容器平台与星辰天合存储产品完成兼容性互认证
  • 谈谈基于Redis的分布式锁
  • 逸学java【初级菜鸟篇】10.I/O(输入/输出)
  • 【Python进阶笔记】md文档笔记第6篇:Python进程和多线程使用(图文和代码)
  • 基于Vue+SpringBoot的数字化社区网格管理系统
  • 【数据库设计和SQL基础语法】--数据库设计基础--数据建模与ER图
  • Vue3 设置点击后滚动条移动到固定的位置
  • 外部 prometheus监控k8s集群资源(pod、CPU、service、namespace、deployment等)
  • LLMLingua:集成LlamaIndex,对提示进行压缩,提供大语言模型的高效推理
  • 数据资产确权的难点
  • EMG肌肉电信号处理合集(二)
  • 2023亚马逊云科技re:Invent引领科技新潮流:云计算与生成式AI共塑未来
  • 案例018:基于微信小程序的实习记录系统
  • 视频剪辑技巧:如何高效批量转码MP4视频为MOV格式
  • node.js获取unsplash图片
  • Git远程库操作(GitHub)
  • java计算下一个整10分钟时间点
  • 力扣刷题篇之排序算法
  • 一键填充字幕——Arctime pro
  • 间隔分区表(DM8:达梦数据库)
  • 基于C#实现并查集