首页 > 编程语言 > 详细

插入排序

时间:2017-07-31 19:46:39      阅读:230      评论:0      收藏:0      [点我收藏+]

一、概念

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

二、复杂度

排序方法 最差时间分析 最好时间分析 平均时间复杂度 空间复杂度 稳定性
选择排序 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     }

技术分享

插入排序

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!