当前位置:网站首页>插入排序
插入排序
2022-06-10 22:38:00 【Li_XiaoJin】
插入排序
插入排序的思路:
- 从第一个元素开始,该元素可以认为已经被排序;
- 取出下一个元素,在前面已排序的元素序列中,从后向前扫描;
- 如果该元素(已排序)大于新元素,将该元素移到下一位置;
- 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置;
- 将新元素插入到该位置后;
- 重复步骤2~5。
https://lixj.fun/upload/2021/07/%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F-ccfb2330d0bf4e04afd9e840b4e99bbc.gif
平均时间复杂度:O(n²)
空间复杂度:O(1)
算法稳定性:稳定
public class InsertSort3 {
public static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int temp = arr[i];
int j = i;
while (j > 0 && arr[j-1] > temp) {
arr[j] = arr[j-1];
j--;
}
if (i != j) {
arr[j] = temp;
}
}
}
public static void main(String[] args) {
int a[] = new int[]{32, 43, 23, 13, 5};
insertSort(a);
for (int i=0; i<a.length; i++) {
System.out.print(a[i] + ",");
}
}
}
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/插入排序
边栏推荐
- Dark horse headlines - Tencent's salary system reform has caused controversy; Intel expanded the recruitment of female engineers nationwide; Black horse is 100% employed really
- Is it safe to open an account in Shanghai Securities?
- Fiddler filtering sessions
- easyrecovery15操作简单方便的数据恢复工具
- LeetCode 501 :二叉搜索樹中的眾數
- Is it safe for BOC securities to open an account? Is it formal?
- How to remove the blank at the top of listview
- 改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年
- Ilruntime hotfix framework installation and breakpoint debugging
- Dell R730 raid5 安装Server 2016(解决磁盘大于2T)
猜你喜欢

Dell R730 raid5 安装Server 2016(解决磁盘大于2T)

BGP - route map extension (explanation + configuration)

【数学】【连续介质力学】流体力学中的对称张量、应变张量和应力张量

LabVIEW and VDM extract color and generate gray image

LabVIEW displays the time and date on the waveform chart or waveform chart

【无标题】

30 | how to reset the consumer group displacement?

黑马头条丨腾讯薪酬制度改革引争议;英特尔全国扩招女工程师;黑马100%就业真的吗......

LeetCode 501 :二叉搜索树中的众数

Serial port missing in Ni Max in LabVIEW
随机推荐
黑马头条丨腾讯薪酬制度改革引争议;英特尔全国扩招女工程师;黑马100%就业真的吗......
[Video] kmeans mean clustering and hierarchical clustering: R language analysis life happiness index visualization | data sharing
Fiddler filtering sessions
Font spider Teaching -- ttf/otf font file compression
Four ways to add names to threads in the thread pool
关于嵌入式音频开发需要注意的两个方面
Classic sentences in Yi Shu's works
OpenResty安装
LabVIEW phase locked loop (PLL)
LabVIEW 禁止其他可多核心处理的应用程序在所有核心上执行
What Fiddler does for testing
[paper sharing] pata: fuzzing with path aware Taint Analysis
Is it safe to open an account online in Shanghai?
Fiddler simulates low-speed network environment
云图说|每个成功的业务系统都离不开APIG的保驾护航
High speed data stream disk for LabVIEW
Example analysis of SQL query optimization principle
Simple impedance matching circuit and formula
苹果CMS采集站源码-搭建教程-附带源码-全新源码-开发文档
Unity 脚本无法显示C#源码的中文注释 或者VS创建的脚本没有C#源码的注释