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

插入排序

时间:2015-07-28 14:31:49      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:

插入排序有N-1趟排序组成,对于p=1到N-1趟,插入排序必须保证位置0到p上的元素为已排序状态。

 1 public static void main(String[] args){
 2         int a[] = {34, 8, 64, 51, 32, 21};
 3         insertionSort(a);
 4         for (int i = 0; i < a.length; i++) {
 5             System.out.print(a[i] + " ");
 6         }
 7     }
 8     
 9     public static void insertionSort(int[] a) {
10         for (int i = 1; i < a.length; i++) {
11             int insertNode = a[i];
12             int j = i-1;
13             while (j >= 0 && insertNode < a[j]) {
14                 a[j+1] = a[j];
15                 j--;
16             }
17             a[j+1] = insertNode;
18         }
19     }

 

插入排序

标签:

原文地址:http://www.cnblogs.com/daemonspirit/p/4682643.html

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