插入排序法学习笔记

时间:2015-05-20 18:03:01   收藏:0   阅读:95

代码:

 1 public class test3
 2 {
 3     public static void insertionSort(int [] a)
 4     {
 5       int i,j,t;
 6       for(i=1;i<a.length;i++)
 7         {
 8           t=a[i];
 9           j=i-1;
10           while(j>=0&&t<a[j])
11             {
12                a[j+1]=a[j];
13                j--;
14            }
15           a[j+1]=t;
16 
17         }
18     
19     }
20 
21     public static void main(String[] args) 
22     {
23         int []a={5,4,3,2,1};
24         insertionSort(a);
25         for(int i=0;i<a.length;i++)
26         {
27                 System.out.println(a[i]);    
28         }
29 
30     }
31 }

运行结果:

技术分享

 

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!