码迷,mamicode.com
首页 > 编程语言 > 详细

插入排序

时间:2017-07-31 20:03:01      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:代码   ext   分析   选择排序   turn   img   blog   ati   main   

一、概念

每次将一个待排序的记录按照关键码的大小插入到一个已经拍好序的有序序列中,直到全部记录拍好序。

二、复杂度

排序方法 最差时间分析 最好时间分析 平均时间复杂度 空间复杂度 稳定性
选择排序 O(n2) O(n) O(n2) O(1) 稳定

三、代码实现

 1 public void insertSort(int[] array){
 2         if(array.length == 0 || array == null)
 3             return;
 4         int temp = 0;
 5         for(int i = 1; i < array.length; i++){
 6             int j = i-1;
 7             temp = array[i];
 8             for(;j >= 0 && temp < array[j];j--){
 9                 array[j+1] = array[j];//后移
10             }
11             array[j+1] = temp;
12             printArray(array,i);
13         }
14     }
15     public void printArray(int a[],int count){
16         if(count != 0)
17         System.out.print("第" + count + "次   ");
18         for(int m = 0; m < a.length; m++){
19             if(count == m && count != 0)
20                 System.out.print("|");
21             System.out.print(a[m] + " ");
22         }
23         System.out.println();
24     }
25     public static void main(String[] args) {
26         int a[] = {11,7,6,1,8,4,3,2};
27         InsertSort is = new InsertSort();
28         is.insertSort(a);
29         Arrays.sort(a);
30     }

技术分享

插入排序

标签:代码   ext   分析   选择排序   turn   img   blog   ati   main   

原文地址:http://www.cnblogs.com/fankongkong/p/7265033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!