插入排序

 1 public class insertSort {
 2     public static void insertSort() {
 3         int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 1};
 4         int temp = 0;
 5         for(int i = 1; i < a.length; i++) {
 6             int j = i - 1;
 7             temp = a[i];
 8             for(; j>=0 && temp<a[j]; j--) {
 9                 a[j+1] = a[j];      //将大于temp的值整体后移一个单位
10             }
11             a[j+1] = temp;
12         }
13         for(int i = 0; i < a.length; i++)
14             System.out.println(a[i]);
15     }
16     public static void main(String[] args) {
17         insertSort();
18     }
19 }

 

posted @ 2016-08-10 17:47  diaoxiankai  阅读(109)  评论(0编辑  收藏  举报