当前位置:网站首页>Direct insertion sort of seven sorts
Direct insertion sort of seven sorts
2022-07-27 22:29:00 【Weiweiqin】
Catalog
2、 Code explanation and Implementation
Time complexity :
~
![]()
Spatial complexity :
stability : Stable
1、 The basic idea
Direct insertion sort is a simple insertion sort method , The idea is to insert the numbers to be sorted into an ordered sequence one by one according to their size , Until all the records are inserted .
In the actual , We are playing cards , It is the same as using the idea of insertion sorting .
2、 Code explanation and Implementation
When inserting the i(i>=1) Element time , Ahead array[0]、array[1].....array[i-1] It's in order .
First step : Define a j Variable and assign to i-1, At the same time define a val Variable storage array[i] Value .
The second step : Give Way array[j] And val Compare
If array[j] Greater than val , Just put array[j] Take a step forward , That is to say array[j+1] = array[j].
If array[j] Less than or equal to val, Just put val Put it in array[j+1] in .
j must Greater than or equal to 0 , Otherwise it will cross the border .

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、 Feature summary
3.1 Time complexity :
When the data is in order , Direct insertion sorting takes the least time , by 
When the data is in order , The outer loop traverses each element , Than before i-1 Both elements are big , Inner loop is equivalent to not executing . The total time is the time to traverse the array .
When the data is in reverse order , It takes the most time , by 
Data in reverse order , Every time you insert the second i Elements , The inner loop must be traversed i-1 Time . When n The number is sorted , It's traversal
Time
3.2 Spatial complexity :
Because direct insertion sort is an internal sort , So the space complexity is zero 
3.3 Suitable for the scene :
When the data is basically orderly , Use direct insert sort
边栏推荐
- fork()函数的执行过程、孤儿进程和僵尸进程
- Behind every piece of information you collect, you can't live without TA
- Window localStorage 属性和Location 对象
- 7行代码让B站崩溃3小时
- SSM整合流程
- Chapter 3 business function development (choose to export market activities, Apache POI)
- Temperature relay
- 8000字讲透OBSA原理与应用实践
- 2022 / July daily report
- The purpose of DDD to divide domains, sub domains, core domains, and support domains
猜你喜欢

Excel only wants to visualize charts and make data move? Yes, come and watch (with a large number of templates to download)

An2021 software installation and basic operation (new file / export)

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

iptables学习

matlab 绘制三坐标(轴)图

七大排序之希尔排序

温度继电器

Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further

An2021软件安装及基本操作(新建文件/导出)

Interview question: talk about your understanding of AQS
随机推荐
SQL injection less26a (Boolean blind injection)
Project analysis (what is it training that can't be given)
SSM integration process
[numerical analysis exercise] Jacobi iteration method of third-order matrix
8000字讲透OBSA原理与应用实践
一种比读写锁更快的锁,还不赶紧认识一下
Inertial navigation principle (VII) -imu error classification (II) -allan variance analysis method +imu test + calibration introduction
First knowledge of esp8266 (I) -- access point and wireless terminal mode
云原生微服务第三章之Haproxy+Keepalived
直播软件app开发,uniapp scroll-view隐藏滚动条
STM32项目分享---MQTT智能门禁系统(含APP控制)
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
If demand splitting is as simple as cutting a cake | agile practice
Maximum sum of jz42 continuous subarray (force buckle) (GIF diagram)
JVM garbage collection garbage collector and common combination parameters
Mimx8md6cvahzab i.MX 8mdual cortex-a53 - Microprocessor
【StoneDB故障诊断】MDL锁等待
【图解】三次握手,四次挥手 —— 用心看这一篇就够了
Matplotlib 多子图绘制
Leetcode-155-minimum stack


Time 
