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

Neo4j 中文开发者月刊 - 202207期

“蔚来杯“2022牛客暑期多校训练营5,签到题KBGHFCD

每周推荐短视频:为什么产品开发需要数字化?如何做到数字化?

论文《Deep Multifaceted Transformers for Multi-objective Ranking in Large-Scale E-commerce Recommender》

Redis 常用命令和基本数据结构(数据类型)

实例027:递归输出

Specified URL is not reachable,caused by :‘Read timed out

(笔记整理未完成)【图论】图的遍历

文件上传漏洞(二)

System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。不可 访问的日志: Security
随机推荐
WebGPU 导入[2] - 核心概念与重要机制解读
Specified URL is not reachable,caused by :‘Read timed out
Wuhan 2022 organizing of the high-performance computing added new ecological development of high-performance computing
SimpleChannelInboundHandler使用总结
php删除一维数组中一个值
Neo4j 中文开发者月刊 - 202207期
实例029:反向输出
【机器学习】实验4布置:AAAI会议论文聚类分析
request.getSession(),的故事
速看!PMP新考纲、PMBOK第七版解读
论文《Deep Multifaceted Transformers for Multi-objective Ranking in Large-Scale E-commerce Recommender》
【ROS基础】rosbag 的使用方法
数据库概论-MySQL的数据表的基本操作
堡垒机、堡垒机的原理
技术管理三级跳
海缆探测仪TSS350(二)
awk语法-01-基础语法(命令、选项、内部变量)
yml字符串读取时转成数字了怎么解决
振兴农村循环经济 和数链串起农业“生态链”
实例032:反向输出II