当前位置: 首页 > news >正文

java-数据结构-插入排序

文章目录

  • 插入排序
  • 插入排序法思想
  • 插入排序思路图
  • 插入排序java代码

插入排序

插入式排序属于内部排序法,是对于欲排序的元素以插入的方式找寻该元素的适当位置,以达到排序的目的。

插入排序法思想

插入排序(Insertion Sorting)的基本思想是:把n个待排序的元素看成为一个有序表和一个无序表,开始时有序表中只包含一个元素,无序表中包含有n-1个元素,排序过程中每次从无序表中取出第一个元素,把它的排序码依次与有序表元素的排序码进行比较,将它插入到有序表中的适当位置,使之成为新的有序表。

插入排序思路图

插入排序思路图

插入排序java代码

package com.sort;

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)); */
		
	}

}

相关文章:

  • java-数据结构-希尔排序
  • java-数据结构-快速排序
  • java-数据结构-归并排序
  • java-数据结构-基数排序(桶排序)
  • java-数据结构-顺序查找
  • java-数据结构-二分查找(折半查找)
  • java-数据结构-插值查找
  • java-数据结构-前中后序遍历
  • java-数据结构-前中后序查找
  • java-数据结构-顺序存储二叉树
  • java-数据结构-线索化二叉树
  • java-数据结构-大顶堆和小顶堆
  • java-数据结构-赫夫曼树(Huffman Tree)
  • java-数据结构-哈夫曼编码(Huffman Coding)
  • java批量修改文件名工具类
  • Docker容器管理
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • HTML-表单
  • Laravel 中的一个后期静态绑定
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • oldjun 检测网站的经验
  • php中curl和soap方式请求服务超时问题
  • React组件设计模式(一)
  • Vue.js 移动端适配之 vw 解决方案
  • 构造函数(constructor)与原型链(prototype)关系
  • 蓝海存储开关机注意事项总结
  • 入口文件开始,分析Vue源码实现
  • 数组大概知多少
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • 小李飞刀:SQL题目刷起来!
  • 写给高年级小学生看的《Bash 指南》
  • 赢得Docker挑战最佳实践
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • (SpringBoot)第二章:Spring创建和使用
  • (二)正点原子I.MX6ULL u-boot移植
  • (翻译)terry crowley: 写给程序员
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (论文阅读笔记)Network planning with deep reinforcement learning
  • (十)c52学习之旅-定时器实验
  • (十六)一篇文章学会Java的常用API
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转)C#调用WebService 基础
  • (转载)hibernate缓存
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .axf 转化 .bin文件 的方法
  • .net 程序发生了一个不可捕获的异常
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
  • ?php echo ?,?php echo Hello world!;?
  • @selector(..)警告提示
  • @SentinelResource详解
  • @WebService和@WebMethod注解的用法
  • [ vulhub漏洞复现篇 ] ThinkPHP 5.0.23-Rce
  • [20161101]rman备份与数据文件变化7.txt
  • [Android View] 可绘制形状 (Shape Xml)