一、用途:

输入:n个数(a1,a2,...,an)

输出:输入序列的一个排列(即重新排序)<a1',a2',...,an'>,使得a1' <= a2' <= ...an'.

待排序的数成为关键字key.

Insertion-sort的参数是一个数组A[1,2,...,N],包含n个待排序的数.(在代码中,A中元素个数n用length[A]表示).输入的个数字是原地排序的(sorted in place),意即这些数字就是在数组A中进行重新排序的.在任何时刻,至多只有其中的常数个数字是存储在数组之外的.dang过程Insertion-sort执行完毕后,输入数组A中就包含了已经排好序的输出序列.

                  

Insertion-sort(A)

1. for j <--- 2 to length[A]

2.   do  key  <--- A[j]

3.     Insert A[j] into the sorted sequence A[1....j-1]

4.     i <--- j-1

5.     while i > 0 and A[i] > key

6.       do A[i+1]  <---  A[i]

7.       i <--- i-1

8.     A[i-1] <--- key

 1 package org.jacky.algorithm.insertion.sort;
 2
 3 public class InsertionSort {
 4     private static final int[] arrays = { 5, 3, 4, 1, 2 };
 5
 6     public static void main(String[] args) {
 7         InsertionSort insertionSort = new InsertionSort();
 8         insertionSort.printArray(arrays);
 9         insertionSort.executeSort(arrays);
10         insertionSort.printArray(arrays);
11     }
12
13     public void executeSort(int[] originalArray) {
14         if (originalArray == null || originalArray.length < 2) {
15             return;
16         }
17         for (int i = 1; i < originalArray.length; i++) {
18             int currentValue = originalArray[i];
19             int position = i;
20             for (int j = i - 1; j >= 0; j--) {
21                 if (originalArray[j] > currentValue) {
22                     originalArray[j + 1] = originalArray[j];
23                     position -= 1;
24                 } else {
25                     break;
26                 }
27             }
28             originalArray[position] = currentValue;
29         }
30     }
31
32     public void printArray(int[] array) {
33         System.out.print("{");
34         for (int i = 0; i < array.length; i++) {
35             System.out.print(array[i]);
36             if (i < array.length - 1) {
37                 System.out.print(", ");
38             }
39         }
40         System.out.println("}");
41     }
42 }

View Code

转载于:https://www.cnblogs.com/Jacky312/p/5209191.html

插入排序算法(insertion-sort)相关推荐

  1. C语言实现折半插入排序(Binary Insertion Sort)算法(附完整源码)

    折半插入排序Binary Insertion Sort 折半插入排序(Binary Insertion Sort)算法的完整源码(定义,实现,main函数测试) 折半插入排序(Binary Inser ...

  2. php代码编写直接插入排序算法,PHP排序算法之直接插入排序(Straight Insertion Sort)实例分析...

    本文实例讲述了PHP排序算法之直接插入排序(Straight Insertion Sort).分享给大家供大家参考,具体如下: 算法引入: 在这里我们依然使用<大话数据结构>里面的一个例子 ...

  3. 插入排序 php,PHP排序算法之直接插入排序(Straight Insertion Sort)实例分析

    本文实例讲述了PHP排序算法之直接插入排序(Straight Insertion Sort).分享给大家供大家参考,具体如下: 算法引入: 在这里我们依然使用<大话数据结构>里面的一个例子 ...

  4. Java排序算法——插入排序(Insertion Sort)

    之前总结了交换排序的冒泡排序与选择排序的简单选择排序,这次我们来看看插入排序的简单插入排序~ 往期传送门: 冒泡排序: Java排序算法--冒泡排序(Bubble Sort)https://blog. ...

  5. js插值法的使用_js 实现排序算法 -- 插入排序(Insertion Sort)

    原文: 插入排序 插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入. 算法描述 ...

  6. 算法:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)和选择排序(Selection Sort)总结...

    背景 这两天温习了 5 中排序算法,之前也都看过它们的实现,因为没有深入分析的缘故,一直记不住谁是谁,本文就记录一下我学习的一些心得. 三种排序算法可以总结为如下: 都将数组分为已排序部分和未排序部分 ...

  7. Python 数据结构与算法——插入排序(insertion sort)

    我们先归纳性地假设前 n−1n-1 个元素已经完成排序,现在要将第 n<script id="MathJax-Element-2" type="math/tex&q ...

  8. 排序——插入排序(Insertion sort)

    算法思想 顾名思义,采用插入的方式,对无序数列进行排序. 维护一个有序区,将数据一个一个插入到有序区的适当位置,直到整个数组都有序.即每步将一个待排序的记录,按其关键码值的大小插入前面已经排序的文件中 ...

  9. 数据结构:插入排序(Insertion sort)

    package com.sortbasic;import java.util.Random;public class InsertionSort {// 数组private static int[] ...

  10. 直接插入排序(Straight Insertion Sort)

    将一个数组,按当前元素的大小,插入到前面已经排好序的数据中的适当位置中, 依次直到全入插入完全. 下面是一个数组在经过插入排序时的变化情况(t表次数times) Init---{7, 4, 3, 2, ...

最新文章

  1. 架构解密:从分布式到微服务
  2. 404 Not Found: Requested route ('jerrylist.cfapps.eu10.hana.ondemand.com') does not exist
  3. Python修饰符--函数修饰符 “@”
  4. 3499元被吐槽太贵!A15加持:新iPhone SE正式发布 还有“苍蝇绿”iPhone 13
  5. R语言︱文本挖掘套餐包之——XML+SnowballC+tm包
  6. 锦囊5-斐波那契数列
  7. 利用Server 2003的远程桌面搭建简易的RAS远程接入系统
  8. 347.前K个高频元素(力扣leetcode) 博主可答疑该问题
  9. 项目:小型局域网的搭建(基础知识+案例)
  10. 形式化验证工具——prism(1-安装)
  11. 查看eclipse是多少位
  12. 【送书啦】Python操作Mysql(连接、数据探查、写Excel)
  13. 华宇软件华为鸿蒙,舒华体育携手华为打造:全球首款搭载鸿蒙操作系统跑步机面世...
  14. arduino环境esp32跑freertos系统实现触摸检测及wifi控制
  15. 形容谣言的四字词语_形容会说谎的四字词
  16. CSS层叠样式表(Cascading Style Sheets)
  17. 使用Sharding-Jdbc进行数据拆分
  18. wpf 非托管代码崩溃_崩溃! 地狱的网络托管公司
  19. 7-4 计算圆柱体的体积
  20. 处理bootstrap-table分页复选框选中获取表单数据

热门文章

  1. python3 socket 接收 bytes 长度 会变,Python解析Socket数据流异常bytes的问题(详细)...
  2. python 计算循环次数,05.Python循环
  3. 彻底搞懂Bert模型
  4. Android每周一轮子:Nvwa(热修复)
  5. 3.Knockout.Js(属性绑定)
  6. 工信部召开地方信息安全工作会议
  7. IPv6 HSRP协议
  8. IDEF企业软件系统建模方法
  9. 一文读懂卷积神经网络(转载)
  10. 打开VS项目时出错 MSBuild。。。