当前位置:网站首页>排序第一节——插入排序(直接插入排序+希尔排序)(视频讲解26分钟)
排序第一节——插入排序(直接插入排序+希尔排序)(视频讲解26分钟)
2022-08-09 06:50:00 【学习追求高效率】
2.1 插入排序(视频讲解26分钟)
直接插入排序+希尔排序
2.1.1基本思想:
直接插入排序是一种简单的插入排序法,其基本思想是:
把待排序的记录按其关键码值的大小逐个插入到一个已经排好序的有序序列中,直到所有的记录插入完为止,得到一个新的有序序列 。实际中我们玩扑克牌时,就用了插入排序的思想。
2.1.2 直接插入排序 | 最坏:O(n^2) 逆序 | 最好:O(n) 有序
当插入第i(i>=1)个元素时,前面的array[0],array[1],…,array[i-1]已经排好序,此时用array[i]的排序码与array[i-1],array[i-2],…的排序码顺序进行比较,找到插入位置即将array[i]插入,原来位置上的元素顺序后移
直接插入排序的特性总结:
- 元素集合越接近有序,直接插入排序算法的时间效率越高
- 时间复杂度:O(N^2)
- 空间复杂度:O(1),它是一种稳定的排序算法
- 稳定性:稳定
模板
/** * 直接插入排序 * 时间复杂度: * 最坏:O(n^2) 逆序 * 最好:O(n) 有序 * 结论:对于直接插入排序来说,数据越有序越快。 * 场景:当数据基本上是有序的时候,使用直接插入排序 * 空间复杂度:O(1) * 稳定性:稳定 * 一个本身就稳定的排序 ,你可以实现为不稳定 * 但是一个本身就不稳定的排序,你没有办法变成稳定的排序 * @param array */
public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int tmp = array[i];
int j = i-1;
for(;j >= 0;j--) {
//加上等号 就是不稳定
if(array[j] > tmp) {
array[j+1] = array[j];
}else {
break;
}
}
array[j+1] = tmp;
}
}
测试耗时 System.currentTimeMillis()
public static void testInsertSort(int[] array) {
array = Arrays.copyOf(array,array.length);
long startTime = System.currentTimeMillis();
Sort.insertSort(array);
long endTime = System.currentTimeMillis();
System.out.println("直接插入排序耗时:"+(endTime-startTime));
}
数组初始化 random.nextInt(1000_0000)
public static void initArrayOrder(int[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = array.length-i;
}
}
public static void initArrayNotOrder(int[] array) {
Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(1000_0000);
}
}
2.1.3 希尔排序( 缩小增量排序 )
希尔排序法又称缩小增量法。希尔排序法的基本思想是:先选定一个整数,把待排序文件中所有记录分成个组,所有距离为的记录分在同一组内,并对每一组内的记录进行排序。然后,取,重复上述分组和排序的工作。当到达=1时,所有记录在统一组内排好序。
希尔排序的特性总结:
- 希尔排序是对直接插入排序的优化。
- 当gap > 1时都是预排序,目的是让数组更接近于有序。当gap == 1时,数组已经接近有序的了,这样就会很
快。这样整体而言,可以达到优化的效果。我们实现后可以进行性能测试的对比。- 希尔排序的时间复杂度不好计算,因为gap的取值方法很多,导致很难去计算,因此在好些树中给出的希尔排
序的时间复杂度都不固定:
《数据结构(C语言版)》— 严蔚敏
《数据结构-用面向对象方法与C++描述》— 殷人昆- 稳定性:不稳定
模板(注意:gap等于几,数据就有几组)
/** * 希尔排序的时间复杂度: * O(N^1.3 - N^1.5) * 空间复杂度:O(1) * 稳定性:不稳定 * @param array */
public static void shellSort(int[] array) {
int gap = array.length;
while (gap > 1) {
shell(array,gap);
gap /= 2;
}
shell(array,1);
/*int[] drr = {5,2,1}; for (int i = 0; i < drr.length; i++) { shell(array,drr[i]); }*/
}
边栏推荐
- 使用百度EasyDL实现智能垃圾箱
- Inception V3 闭眼检测
- ByteDance Written Exam 2020 (Douyin E-commerce)
- Thread Pool Summary
- 2017.10.26模拟 b energy
- 【MySQL】update mysql.user set authentication_string=password(“123456“) where User=‘root‘; 报错
- 代码目录结构
- Leetcode 70 stairs issues (Fibonacci number)
- leetcode:55. 跳跃游戏
- 推进产教融合 赋能教育创新发展 | 华云数据荣获“企业贡献奖”
猜你喜欢
Fragments
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道
Fragments
The JVM thread state
常见的分布式事务解决方案
The water problem of leetcode
一道很简答但是没答对的SQL题
Use baidu EasyDL intelligent bin
图论,二叉树,dfs,bfs,dp,最短路专题
The solution that does not work and does not take effect after VScode installs ESlint
随机推荐
DDD 领域驱动设计
ByteDance Written Exam 2020 (Douyin E-commerce)
XxlJobConfig分布式定时器任务管理XxlJob配置类,替代
jdepend
CMake中INSTALL_RPATH与BUILD_RPATH问题
P7 Alibaba Interview Questions 2020.07 Sliding Window Algorithm (Alibaba Cloud Interview)
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道
分布式理论
Fragments
使用百度EasyDL实现智能垃圾箱
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
MongDb query method
The division principle summary within the collection
单例 DCL(double check lock) 饱汉模式和饿汉模式
imageio读取.exr报错 ValueError: Could not find a backend to open `xxx.exr‘ with iomode `r`
高项 04 项目变更管理
stm32定时器之简单封装
leetcode:55. 跳跃游戏
mysql summary
failed (13: Permission denied) while connecting to upstream