当前位置:网站首页>直接插入排序——【常见排序法(1/8)】
直接插入排序——【常见排序法(1/8)】
2022-06-23 03:53:00 【东莞呵呵】
目录
1、思路
1、我们把第一个数当成有序,后面的就是无序区域
2、从1下标开始依次与前面有序部分相比较,直到小于该元素或是已经比较完停止
3、插入该元素到停止时的位置
2、演示

3、 属性
1、时间复杂度
最好是O(n)待排数列已经有序。
最差是O(n^2)待排数列逆序
2、空间复杂度
空间复杂度是O(1),没有创建新的数列
3、稳定性
稳定
4、原码
public class TestDemo1 {
public static void insertSort(int[] nums){
for (int i = 1; i < nums.length; i++) {
int tmp=nums[i];
int j = i - 1;
for (; j >= 0 ; j--) {
if(nums[j]>tmp){//如果加入等号之后就不稳定
nums[j+1]=nums[j];
}else{
break;
}
}
nums[j+1]=tmp;
}
}
public static void main(String[] args) {
int[] nums={23,6,5,34,38,3,32,48};
insertSort(nums);
System.out.println(Arrays.toString(nums));
}
}
边栏推荐
- Notepad++ find replace group replace retain
- Usage of API interface test ------ post
- ApiPost接口测试的用法之------Post
- Abnova blood total nucleic acid purification kit protocol
- What are the types of independent station chat robots? How to quickly create your own free chat robot? It only takes 3 seconds!
- What are the main aspects of visual improvement brought by introducing AI into ISP Technology
- Shadertoy basic teaching 01. Circle drawing (explanation of smoothstep() function)
- ICER skill 01 regular matching
- Composer by installation laravel
- 微信小程序:拼图工具箱
猜你喜欢

Can bus Basics

VGg Chinese herbal medicine identification

Arduino温湿度传感器DHT11(含代码)

ApiPost接口测试的用法之------Post

ICER skills 03design compile

Parameter passing of 18 generator function

Arduino火焰传感器(含代码)

微信小程序;AI智能配音助手

Flask Foundation: environment setup + configuration + mapping between URL and attempt + redirection + database connection

【毕业季_进击的技术er】送别过去两年迷茫的自己。重整旗鼓,大三我来啦
随机推荐
Emergency response HW review
Three operation directions of integral mall
Cloud function realizes fuzzy search function
centos7安装postgresql8.2.15及存储过程创建
元数据管理Apache Atlas编译(内嵌式)部署及遇到各种错误记录
Can bus Basics
ICer技能02makefile脚本自跑vcs仿真
dolphinscheduler 2.0.5 spark 任务测试总结(源码优化)
使用PX4的ECL进行多传感器数据融合的后处理
接收传来得文件并下载(简单用法)a标签
硬核,成为高素质测试人员:学会和产品沟通需求
笔者认为所谓的产业互联网,就是一个产业与互联网深度融合的过程
Thinkphp6 solving jump problems
STL tutorial 3- exception mechanism
【毕业季_进击的技术er】送别过去两年迷茫的自己。重整旗鼓,大三我来啦
功能测试人员如何做到花一个月的时间进阶自动化软件测试工程师
WPF 基础控件之 TabControl样式
微信小程序;AI智能配音助手
Kail infiltration basic literacy basic command
ICER skill 01 regular matching