当前位置:网站首页>七大排序之直接插入排序
七大排序之直接插入排序
2022-07-27 19:35:00 【威威沁沁】
目录
时间复杂度:
~
![]()
空间复杂度:
稳定性: 稳定
1、基本思想
直接插入排序是一种简单的插入排序法,其思想就是把待排序的数按照其大小逐个插入到一个已将排好的有序序列中,直到所有的记录插入完为止。
实际中,我们在玩扑克牌时,就与运用了插入排序的思想。
2、代码讲解和实现
当插入第i(i>=1)个元素时,前面的array[0]、array[1].....array[i-1] 已经排好序。
第一步:定义一个 j 变量并赋值为i-1,同时定义一个 val 变量存储一下 array[i]的值。
第二步:让array[j] 与 val 进行比较
如果 array[j] 大于 val , 就把array[j]向前挪一步,也就是 array[j+1] = array[j]。
如果 array[j] 小于等于 val,就把 val 放到 array[j+1]中。
j 必须 大于等于0 ,要不然会越界。

public static void insertSort(int[] array){
for (int i = 1; i < array.length; i++) {
int j = i-1;
int val = array[i];
for(; j >= 0 ; j--){
if(array[j] > val){
array[j+1] = array[j];
}else{
break;
}
}
array[j+1]= val;
}
}3、特性总结
3.1 时间复杂度:
当数据有序时,直接插入排序耗时最少,为 
数据有序时,外循环每遍历一个元素,都比前 i-1个元素都大,内循环相当于没有执行。总的时间就是遍历数组的时间。
当数据逆序时,耗时最多,为 
数据逆序,每次插入第 i 个元素,内循环都要遍历 i-1 次。当 n 数排序完,也就是遍历了
次
3.2 空间复杂度:
因为直接插入排序是内部排序,所以空间复杂度为 
3.3 适合场景:
当数据基本上是有序的时候,使用直接插入排序
边栏推荐
- [numerical analysis exercise] Jacobi iteration method of third-order matrix
- Are Transformers Effective for Time Series Forecasting?| Pit filling
- Station B collapsed. If we were the developer responsible for the repair that night
- JVM garbage collection garbage collector and common combination parameters
- Simple use of enum
- 【海洋科学】海洋气候指数【Climate Indices】数据集
- Understanding of L1 regularization and L2 regularization [easy to understand]
- 刚培训完的中级测试工程师如何快速度过试用期
- CMOS传输门原理及应用
- EC code introduction
猜你喜欢
![[Marine Science] climate indices data set](/img/0f/f63b946b80fc2cf1d39a975c318abc.png)
[Marine Science] climate indices data set

高频继电器

What is the employment prospect of software testing?

ThreadLocal principle and source code analysis (click in step by step, don't recite, learn ideas)

Station B collapsed. What did the developer responsible for the repair do that night?

leetcode383赎金信

华能福建公司与华为数字能源深化战略合作,共建低碳智能县域

What is eplato cast by Plato farm on elephant swap? Why is there a high premium?

Leetcode383 ransom letter

Is log4j vulnerability still widespread?
随机推荐
MySQL series - database tables, queries, sorting, and data processing functions
Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing
刚培训完的中级测试工程师如何快速度过试用期
Log4j vulnerability is still widespread and continues to cause impact
Cloud native microservices Chapter 3: haproxy+kept
九天后我们一起,聚焦音视频、探秘技术新发展
【sql】SQL优化
Deepfake 换脸真假难辨,马斯克分克已伪装成功
Project analysis (from technology to project and product)
How to quickly pass the probation period for newly trained intermediate test engineers
8000字讲透OBSA原理与应用实践
Project analysis (what is it training that can't be given)
8000 word explanation of OBSA principle and application practice
Matplotlib multi subgraph drawing
vs2019 release模式调试:此表达式有副作用,将不予计算。
Leetcode 301. delete invalid parentheses
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
C language output teaching calendar
What is the employment prospect of software testing?
A lock faster than read-write lock. Don't get to know it quickly


次
