当前位置:网站首页>2.3插入排序
2.3插入排序
2022-07-29 11:41:00 【TUJC】
2.3、插入排序
2.3.1、基本介绍
插入排序(Insertion Sorting属于内部排序法,是对于欲排序的元素以插入的方式找寻该元素的适当位置,以达到排序的目的。
插入排序法思想:
把n个待排序的元素看成为一个有序表和一个无序表,开始时有序表中只包含一个元素,无序表中包含有n-1个元素,排序过程中每次从无序表中取出第一个元素,把它的排序码依次与有序表元素的排序码进行比较,将它插入到有序表中的适当位置,使之成为新的有序表。
插入排序思路图:
2.3.2、代码实例
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class InsertSort {
public static void main(String[] args) {
//int[] arr = {101, 34, 119, 1, -1, 89};
// 创建要给80000个的随机的数组
int[] arr = new int[80000];
for (int i = 0; i < 80000; i++) {
arr[i] = (int) (Math.random() * 8000000); // 生成一个[0, 8000000) 数
}
System.out.println("插入排序前");
Date data1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1Str = simpleDateFormat.format(data1);
System.out.println("排序前的时间是=" + date1Str);
insertSort(arr); //调用插入排序算法
Date data2 = new Date();
String date2Str = simpleDateFormat.format(data2);
System.out.println("排序前的时间是=" + date2Str);
//System.out.println(Arrays.toString(arr));
}
//插入排序
public static void insertSort(int[] arr) {
int insertVal = 0;
int insertIndex = 0;
//使用for循环来把代码简化
for(int i = 1; i < arr.length; i++) {
//定义待插入的数
insertVal = arr[i];
insertIndex = i - 1; // 即arr[1]的前面这个数的下标
// 给insertVal 找到插入的位置
// 说明
// 1. insertIndex >= 0 保证在给insertVal 找插入位置,不越界
// 2. insertVal < arr[insertIndex] 待插入的数,还没有找到插入位置
// 3. 就需要将 arr[insertIndex] 后移
while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
arr[insertIndex + 1] = arr[insertIndex];// arr[insertIndex]
insertIndex--;
}
// 当退出while循环时,说明插入的位置找到, insertIndex + 1
// 举例:理解不了,我们一会 debug
//这里我们判断是否需要赋值
if(insertIndex + 1 != i) {
arr[insertIndex + 1] = insertVal;
}
//System.out.println("第"+i+"轮插入");
//System.out.println(Arrays.toString(arr));
}
/* //使用逐步推导的方式来讲解,便利理解 //第1轮 {101, 34, 119, 1}; => {34, 101, 119, 1} //{101, 34, 119, 1}; => {101,101,119,1} //定义待插入的数 int insertVal = arr[1]; int insertIndex = 1 - 1; //即arr[1]的前面这个数的下标 //给insertVal 找到插入的位置 //说明 //1. insertIndex >= 0 保证在给insertVal 找插入位置,不越界 //2. insertVal < arr[insertIndex] 待插入的数,还没有找到插入位置 //3. 就需要将 arr[insertIndex] 后移 while(insertIndex >= 0 && insertVal < arr[insertIndex] ) { arr[insertIndex + 1] = arr[insertIndex];// arr[insertIndex] insertIndex--; } //当退出while循环时,说明插入的位置找到, insertIndex + 1 //举例:理解不了,我们一会 debug arr[insertIndex + 1] = insertVal; System.out.println("第1轮插入"); System.out.println(Arrays.toString(arr)); //第2轮 insertVal = arr[2]; insertIndex = 2 - 1; while(insertIndex >= 0 && insertVal < arr[insertIndex] ) { arr[insertIndex + 1] = arr[insertIndex];// arr[insertIndex] insertIndex--; } arr[insertIndex + 1] = insertVal; System.out.println("第2轮插入"); System.out.println(Arrays.toString(arr)); //第3轮 insertVal = arr[3]; insertIndex = 3 - 1; while (insertIndex >= 0 && insertVal < arr[insertIndex]) { arr[insertIndex + 1] = arr[insertIndex];// arr[insertIndex] insertIndex--; } arr[insertIndex + 1] = insertVal; System.out.println("第3轮插入"); System.out.println(Arrays.toString(arr)); */
}
}
边栏推荐
- DNS protocol, ICMP protocol, NAT technology
- 报表查询字段集sql摘记
- Dawei gbase8s cursor stability read esql test case
- 北京大学公开课重磅来袭!欢迎走进「AI for Science」课堂
- INVALID_ ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
- After connect and SQL join in on conditions and where
- QML(一):自定义圆角按钮的处理
- Function comparison between report control FastReport and stimulus soft
- 『知识集锦』一文搞懂mysql索引!!(建议收藏)
- 就这?TypeScript其实并不难!(建议收藏)
猜你喜欢

What is kubernetes custom resource definition (CRD)?

QWidget、QDialog、QMainWindow 的异同点
小笑授权系统V5.0开心版

One click blog building: how to use WordPress plug-in to build a dedicated blog

暑假集训week1

考完PMP后有什么益处

如何对SQuAD1.1数据集进行预处理「详解版」

std::vector 拷贝、追加、嵌套访问

精通音视频开发是真的可以为所欲为
![[image processing] image skeleton extraction based on central axis transformation with matlab code](/img/34/80e777c5c0a2a791acd0892e3e0b04.png)
[image processing] image skeleton extraction based on central axis transformation with matlab code
随机推荐
【表达式计算】表达式计算问题的通用解法(练习加强版,含总结)
mapbox 地图 生成矢量数据圆
Meituan and hungry were interviewed by Hangzhou supervisors to implement the responsibility of food safety management and prohibit malicious competition
Collections.singletonList(T o)
黑马四小时入门学习记录-2|本地应用
使用anyio替代asyncio
微信云托管入门与实践
【Unity3D】场景切换、退出全屏、退出游戏
mysql单行,多行子查询
建议收藏丨sql行转列的一千种写法!!
Lucky draw system with background source code
幸运抽奖系统带后台源码
文件上传漏洞
Gbase8s core data backup
DNS protocol, ICMP protocol, NAT technology
Std:: vector copy, append, nested access
WeChat red envelope test case
Alluxio为Presto赋能跨云的自助服务能力
Function comparison between report control FastReport and stimulus soft
Paddlelite compilation and code running through the disk