标签:
代码:
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 }
运行结果:
标签:
原文地址:http://www.cnblogs.com/gugibv/p/4517568.html