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

插入排序

时间:2016-08-10 19:22:44      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

插入排序

标签:

原文地址:http://www.cnblogs.com/diaoxiankai/p/5757748.html

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