1、介绍
插入排序(Insertion Sorting)属于内部排序法,是对于欲排序的元素以插入的方式找寻该元素的适当位置,以达到排序的目的。
2、算法分析
把 n 个待排序的元素看成为一个有序表和一个无序表,开始时有序表中只包含一个元素,无序表中包含有 n-1 个元素,排序过程中每次从无序表中取出第一个元素,把它的排序码依次与有序表元素的排序码进行比较,将它插入到有序表中的适当位置,使之成为新的有序表,直到无序表中的元素都插入到有序表中。
3、代码实现
package com.czq.algorithm;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
/**
* 插入排序
*
* @author czq
* @date 2020/07/17
*/
public class InsertSort {
public static void main(String[] args) {
//测试排序的执行速度
//创建要给 800000 个的随机的数组
int[] arr = new int[800000];
for (int i = 0; i < arr.length; i++) {
// 生成一个[0, 8000000) 数
arr[i] = (int) (Math.random() * 8000000);
}
// int arr[] = {8, 3, 2, 1, 7, 4, 6, 5};
System.out.println("============排序前===============");
// System.out.println("排序后:" + Arrays.toString(arr));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("排序前时间:" + sdf.format(new Date()));
//移动法
insertSort_move(arr);
//交换法
// insertSort_exchange(arr);
System.out.println("============排序后===============");
System.out.println("排序前时间:" + sdf.format(new Date()));
// System.out.println("排序后:" + Arrays.toString(arr));
}
/**
* 选择排序分析方法
*
* @param arr 排序的原始数组
*/
private static void selectSortAnalysis_move(int[] arr) {
/*************************第1轮****************************/
//待插入的数
int insertVal = arr[1];
// 待插入数前面这个数的下标
int insertIndex = 1 - 1;
//1. insertIndex >= 0 保证在给 insertVal 找插入位置,不越界
//2. insertVal < arr[insertIndex] 待插入的数,还没有找到插入位置
//3. 就需要将 arr[insertIndex] 后移一位
while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
arr[insertIndex + 1] = arr[insertIndex];
insertIndex--;
}
//当退出 while 循环时,说明找到插入的位置, insertIndex + 1
//判断是否需要赋值
if ((insertIndex + 1) != 0) {
arr[insertIndex + 1] = insertVal;
}
System.out.println("第 1 轮后:" + Arrays.toString(arr));
/*************************第2轮****************************/
insertVal = arr[2];
insertIndex = 2 - 1;
while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
arr[insertIndex + 1] = arr[insertIndex];
insertIndex--;
}
if ((insertIndex + 1) != 1) {
arr[insertIndex + 1] = insertVal;
}
System.out.println("第 2 轮后:" + Arrays.toString(arr));
/*************************第3轮****************************/
insertVal = arr[3];
insertIndex = 3 - 1;
while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
arr[insertIndex + 1] = arr[insertIndex];
insertIndex--;
}
if ((insertIndex + 1) != 2) {
arr[insertIndex + 1] = insertVal;
}
System.out.println("第 3 轮后:" + Arrays.toString(arr));
}
/**
* 插入排序--移动法
*
* @param arr 排序的原始数组
*/
private static void insertSort_move(int[] arr) {
//假定数组第一个数有序,从第二个数开始进行插入排序
for (int i = 1; i < arr.length; i++) {
//待插入的数
int insertVal = arr[i];
// 待插入数前面这个数的下标
int insertIndex = i - 1;
//1. insertIndex >= 0 保证在给 insertVal 找插入位置,不越界
//2. insertVal < arr[insertIndex] 待插入的数,还没有找到插入位置
//3. 就需要将 arr[insertIndex] 后移一位
while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
arr[insertIndex + 1] = arr[insertIndex];
insertIndex--;
}
//当退出 while 循环时,说明找到插入的位置, insertIndex + 1
//判断是否需要赋值
if ((insertIndex + 1) != i) {
arr[insertIndex + 1] = insertVal;
}
}
}
/**
* 插入排序--交换法,效率比较低
*
* @param arr 排序的原始数组
*/
private static void insertSort_exchange(int[] arr) {
//假定数组第一个数有序,从第二个数开始进行插入排序,这样可以看作左边为有序表,右边为无序表
int temp = 0;
//向右遍历无序表
for (int i = 1; i < arr.length; i++) {
//向左遍历有序表
for (int j = i - 1; j >= 0; j--) {
//如果相邻两数,右边比左边小,则交换
if (arr[j + 1] < arr[j]) {
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}
}
移动法结果:80000长度的数组排序时间需要1~2秒,时间复杂度O(n2),空间复杂度O(1)
800000长度的数组排序时间需要1分钟左右
交换法结果:80000长度的数组排序时间需要4~5秒,时间复杂度O(n2),空间复杂度O(1)
800000长度的数组排序时间需要6~7分钟