当前位置:网站首页>21 days learning challenge 】 【 sequential search
21 days learning challenge 】 【 sequential search
2022-08-02 07:38:00 【Taiyi TT】
One-dimensional array element search method
顺序查找
Start with one side of the array,Perform element-by-element comparisons,The search succeeds until the element to be searched is the same as the given element,If no matching element is found after the search is complete, the search fails.
二分查找
Under the premise of orderly calculation,Keep it as narrow as possible,提高查找效率.
索引查找
对于无序的数据集合,Create an index table first,Yes the index table is ordered or chunked ordered,The lookup is done by combining sequential lookup and index lookup.
顺序查找
输入:n个数字(无序).
输出:If the search is successful, the subscript is output,失败则返回-1.
int nums[10],x;
cin>>x;
for(int i=0;i<nums.size();i++){
if(x==nums[i]) return i;
return -1;
The space complexity is only introduced abovei这个变量为O(1).
The time complexity is worst caseO(n)
插入排序
原理:每次插入一个元素,Each pass completes the placement of an element to be queued,直到全部插入完成.
直接插入排序
将每个待排元素Insert into knownalready arranged sequence中.(Each time a new arrangement is taken from the original data,Insert into a previously arranged sequence,until all numbers are taken,This new arrangement is complete)
算法详解:
假设前面 n-1(其中 n>=2)个数已经是排好顺序的,现将第 n 个数插到前面already arranged sequence中,然后找到合适自己的位置,使得插入第n个数的这个序列也是排好顺序的.
从小到大的插入排序整个过程如图示:
第一轮:从第二位置的 6 开始比较,比前面 7 小,交换位置.
第二轮:第三位置的 9 比前一位置的 7 大,无需交换位置.
第三轮:第四位置的 3 比前一位置的 9 小交换位置,依次往前比较.
第四轮:第五位置的 1 比前一位置的 9 小,交换位置,再依次往前比较.
public class InsertionSort {
public static void sort(Comparable[] arr){
int n = arr.length;
for (int i = 0; i < n; i++) {
// 寻找元素 arr[i] 合适的插入位置
for( int j = i ; j > 0 ; j -- )
if( arr[j].compareTo( arr[j-1] ) < 0 )
swap( arr, j , j-1 );
else
break;
}
}
//核心代码---结束
}
}
折半插入排序
Due to the insertion sort process,One has been generated有序序列.So when inserting elements to be sorted, it is faster to use halved searchDetermine the position of the new element.when the number of elements is large,Half-insertion sort is better than direct insertion sort.
希尔排序
After dividing all elements into groups,Use inline insertion sort within each group,Then continue to reduce the spacing,Form a new group for sorting,直到间距为0为止.
边栏推荐
- php删除一维数组中一个值
- Revitalize rural circular economy and digital chain to link agricultural "ecological chain"
- Connection reset by peer 问题解析
- 2022夏暑假每日一题(六)
- 【机器学习】课程设计布置:某闯关类手游用户流失预测
- 关于ue4.27像素流送打包后的本地服务器问题
- (笔记整理未完成)【图论】图的遍历
- 解决:- SPY: No data found for this date range, symbol may be delisted报错
- typescript ‘props‘ is declared but its value is never read 解决办法
- 根据一个字段的内容去更新另一个字段的数据,这样的sql语句该怎么样书写
猜你喜欢
实例031:字母识词
【云原生】如何快速部署Kubernetes
实例029:反向输出
Wuhan 2022 organizing of the high-performance computing added new ecological development of high-performance computing
(Notes are not completed) [Graph Theory] Traversal of graphs
chrome 插件开发指南
How does abaqus quickly import the assembly of other cae files?
你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
深度学习网络模型的改进与调整
入门opencv,欢笑快乐每一天
随机推荐
张驰咨询:企业实施精益管理的最大障碍,只把精益作为一种工具和方法
速看!PMP新考纲、PMBOK第七版解读
2022.07.31(LC_6133_分组的最大数量)
【故障诊断分析】基于matlab FFT轴承故障诊断【含Matlab源码 2001期】
2022夏暑假每日一题(六)
张驰课堂:六西格玛测量系统的误差分析与判定
封装class类一次性解决全屏问题
振兴农村循环经济 和数链串起农业“生态链”
两篇不错的php debug教程
[Dataset][VOC] Male and female dataset voc format 6188 sheets
【机器学习】实验5布置:AAAI会议论文聚类分析
根据一个字段的内容去更新另一个字段的数据,这样的sql语句该怎么样书写
request.getSession(), the story
Summer Summary (3)
nacos源码启动找不到istio包
Revitalize rural circular economy and digital chain to link agricultural "ecological chain"
【CNN回归预测】基于matlab卷积神经网络CNN数据回归预测【含Matlab源码 2003期】
第06章 索引的数据结构【2.索引及调优篇】【MySQL高级】
实例027:递归输出
实例028:递归求等差数列