插入排序实现

 1 public class InsertSort {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 // TODO Auto-generated method stub
8 int []ary =new int[]{2,5,1,4,8,3,6,9};
9 //int []ary={2,5,1,4,8,3,6,9};
10 ary=insertSort(ary);
11 for(int i=0;i<ary.length;i++)
12 {
13 System.out.print(ary[i]+" ");
14 }
15 }
16
17 public static int [] insertSort(int []ary)
18 {
19 for(int i=1;i<ary.length;i++)
20 {
21 int temp=ary[i];
22 int j;
23 //for(j=i-1;j>=0&&temp<ary[j];j--)
24 for(j=i-1;j>=0;j--)
25 {
26 if(temp<ary[j])
27 {
28 ary[j+1]=ary[j];
29 }else
30 break;
31 }
32 ary[j+1]=temp;//插入
33 }
34 return ary;
35 }
36 }

posted @ 2011-07-26 20:48  牧涛  阅读(173)  评论(0编辑  收藏  举报