数据结构-插入排序InsertSort
来源:互联网 发布:mongodb python 教程 编辑:程序博客网 时间:2024/05/19 14:54
多数情况下:插入排序在简单排序中是最好的一种,虽然算法需要的时间仍然是O(N^2),但是一般情况下比冒泡排序快一倍,比选择排序要快,但是插入排序要麻烦一点,但是它常用在较复杂的排序的最后阶段。
概念:对于给定的一组随机数据,初始时假设第一个记录自成一个有序序列,其余为无序的,从第二个记录开始,按照记录的大小一次将当前处理的记录插入到之前的有序序列,直到最后一个为止。
3 2 1 6 4 8 7
第一次排序:【2 3 】1 6 4 8 7
第二次排序:【1 2 3】 6 4 8 7
第三次排序:【1 2 3 6】4 8 7
第四次排序:【1 2 3 4 6 】 8 7
第五次排序:【1 2 3 4 6 8】7
第六次排序:【1 2 3 4 6 7 8】
Java代码:
public class Arr {public static void main(String[] args) {int b[]={6,12,1,16,7,5,1};InsertSort(b);for(int i=0;i<b.length;i++){System.out.print(b[i]+" ");}}public static void InsertSort(int []a){ int temp=0,j; for(int i=1;i<a.length;i++){ //保存每次排序后的下一值, temp=a[i];j=i; //如果保存的值比牌后最大值小,可开始循环比较,将保存的值放入到合适的位置 if(temp<a[j-1]){ while(j>=1&&a[j-1]>temp){ //将需要排序的值左移动,直到位置合适。 a[j]=a[j-1]; j--; } } //如果保存的值比牌后最大值大,则直接放入排序后的最后一个位置 a[j]=temp; }}}插入排序:就是不断的将需要排序的数据放入到已经排序的序列中来。
1 0
- 数据结构-插入排序InsertSort
- 【数据结构】中的插入(InsertSort)排序
- 算法与数据结构--插入排序(InsertSort)
- 插入排序InsertSort
- insertSort - 插入排序
- 插入排序 InsertSort
- 插入排序 InsertSort
- 插入排序(InsertSort)
- 插入排序InsertSort
- 插入排序-InsertSort
- java insertsort 插入排序
- 插入排序-insertsort
- 插入排序(InsertSort)
- 插入排序(InsertSort)
- InsertSort 插入排序算法
- InsertSort(插入排序)
- 数据结构--插入排序(InsertSort)思想与实现
- 排序算法-插入排序 insertSort
- Couldnot publish server configuration for MyEclipse Tomcat v7.0.
- 1028. 人口普查(20)
- 1029. 旧键盘(20)
- 计算语言学之句法理论(1)
- mongo的使用
- 数据结构-插入排序InsertSort
- python时间,日期,时间戳处理
- 1030. 完美数列(25)
- 计蒜客 三值排序(贪心)
- C++访问注册表获取已安装软件信息列表示例代码
- 1031. 查验身份证(15)
- HDU
- 1032. 挖掘机技术哪家强(20)
- swift中枚举的定义以及调用传值的小功能,