当前位置:网站首页>【21 Days Learning Challenge】Direct Insertion Sort
【21 Days Learning Challenge】Direct Insertion Sort
2022-08-04 04:16:00 【Alex holding popcorn】
活动地址:CSDN21天学习挑战赛
怕什么真理无穷,进一步有一份的欢喜.
目录
【21天学习挑战赛】直接插入排序
我为什么参与挑战赛
1,机缘
读到研一了,暑假器件打开私信发现这个挑战赛就鼓起勇气参加了.
2,期待的收获
A, 本人在华南理工大学攻读专硕,目前研究方向是图像恢复,从事深度学习相关工作,目标是从事Java后端开发.
B, 期待您的反馈,如果有什么不对的地方,欢迎指正!
C, 期待认识志同道合的领域同行或技术交流.
如果感觉博主的文章还不错的话,还请关注、点赞、收藏三连支持一下博主哦
什么是排序?
在计算机科学中定义为:In the computer, a group is divided according to certain rules“无序”的数据元素调整为“有序”的数据元素.也就是说,我们是出于某种目的,Make it regular for a bunch of data,Generally divided into ascending and descending order.
- 升序:按从小到大的顺序排列(如1、3、5、6、7、9).
- 降序:就是按从大到小的顺序排列(如9、8、6、4、3、1).
Definition of Insertion Sort
将一条记录插入到已排好的有序表中,从而得到一个新的、记录数量增1的有序表.
Pros and cons of direct insertion sort
优势
简单,Insert data into an ordered table.
劣势
Direct insertion sort is made by 两层嵌套循环组成的.时间复杂度高,时间效率是O(n^2).
Steps for direct insertion sort
- 每次从无序表中取出第一个元素,把它插入到有序表的合适位置,使有序表仍然有序. 第一趟比较前两个数,然后把第二个数按大小插入到有序表中;
- 第二趟把第三个数据与前两个数从后向前扫描,把第三个数按大小插入到有序表中;依次进行下去,进行了(n-1)趟扫描以后就完成了整个排序过程.
️ 算法实现
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
int[] arr = {
9, 20, 15, 37, 28, 23};
DirectInsertSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void DirectInsertSort(int[] arr) {
//前第0位-第i-1位是有序的,如果第iBits are not in order for bit-seeking insertion,
// Ignore if ordered,judge the next.
for (int i = 1; i < arr.length; i++) {
if (arr[i] < arr[i - 1]) {
int key = arr[i];
int j = i - 1;
//遍历前i-1位,寻找合适的位置
while (j >= 0 && key < arr[j]){
arr[j+1] = arr[j];
j--;
}
arr[j+1]=key;//插入第i位的值
}
}
}
}
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
️ 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
- 7-2 LVS+DR概述与部署
- A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
- 技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
- 拿捏JVM性能优化(自己笔记版本)
- PL/SQL Some Advanced Fundamental
- 结构体函数练习
- mq应用场景介绍
- Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
- 【C语言进阶】程序环境和预处理
- y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)
猜你喜欢
Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model
if,case,for,while
The general SQL injection flow (sample attached)
SQL injection in #, - +, - % 20, % 23 is what mean?
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
2022年软件测试——精选金融银行面试真题
7-3 LVS+Keepalived Cluster Description and Deployment
TL431的基本特性以及振荡电路
y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)
Learn iframes and use them to solve cross-domain problems
随机推荐
Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
8.Haproxy 搭建Web集群
Basic characteristics of TL431 and oscillator circuit
pnpm 是凭什么对 npm 和 yarn 降维打击的
图像处理之Bolb分析(一)
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
帮助企业实现数字化转型成功的八项指导原则
目标检测-中篇
十一种概率分布
如何动态添加script依赖的脚本
mysql index notes
Learn iframes and use them to solve cross-domain problems
文件系统的简单操作
manipulation of file contents
2022杭电多校联赛第五场 题解
Hey, I had another fight with HR in the small group!
8. Haproxy builds a web cluster
typescript type 和 interface 的区别