当前位置:网站首页>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为止.
边栏推荐
猜你喜欢

chrome 插件开发指南

Unity Shader学习(七)纹理图像的简单使用

实例031:字母识词

Day 4 of HCIP

Neo4j 中文开发者月刊 - 202207期

PMP新考纲考试内容介绍

CAT1 4G+Ethernet development board Tencent cloud mobile phone WeChat applet display temperature and delivery control
——设备树的概述(硬件、目标、效果、文件类型)](/img/c6/6c2321bfcd184886e1cb59664bec11.png)
[21天学习挑战赛——内核笔记](一)——设备树的概述(硬件、目标、效果、文件类型)

typescript ‘props‘ is declared but its value is never read 解决办法
![[Dataset][VOC] Male and female dataset voc format 6188 sheets](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Dataset][VOC] Male and female dataset voc format 6188 sheets
随机推荐
笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
【暑期每日一题】洛谷 P3156 【深基15.例1】询问学号
[21天学习挑战赛——内核笔记](一)——设备树的概述(硬件、目标、效果、文件类型)
PWA 踩坑 - 第一次加载页面后无法获取CacheStorage某些资源
About the local server problem after ue4.27 pixel streaming package
倍福使用AdsRemote组件实现和C#的ADS通讯
有趣的网站
(笔记整理未完成)【图论】图的遍历
分离轴定理SAT凸多边形精确碰撞检测
暑假第五周总结
jvm 二之 栈帧内部结构
【机器学习】实验1布置:基于决策树的英雄联盟游戏胜负预测
打卡day05
【机器学习】实验5布置:AAAI会议论文聚类分析
【机器学习】实验3布置:贝叶斯垃圾邮件识别
封装class类一次性解决全屏问题
optional
How does abaqus quickly import the assembly of other cae files?
关于ue4.27像素流送打包后的本地服务器问题
PHP Warning: putenv() has been disabled for security reasons in phar