1 //升序插入排序
2 public static int[] insertSort(int[] a) {
3 for(int i = 1; i < a.length; i++) {
4 int j = i-1;
5 int tmp = a[i];
6 while(j >= 0 && a[j] > tmp) {
7 a[j+1] = a[j];
8 j = j -1;
9 }
10 a[j+1] = tmp;
11
12 }
13
14 return a;
15 }
16
17 //降序插入排序
18 public static int[] insertSortDesc(int[] a) {
19 for(int j=1; j < a.length; j++) {
20 int i = j - 1;
21 int key = a[j];
22 while(i >= 0 && a[i] < key) {
23 a[i+1] = a[i];
24 --i;
25 }
26 a[i+1] = key;
27 }
28 return a;
29 }
posted on 2011-05-25 14:02
wawlian 阅读(167)
评论(0) 编辑 收藏 所属分类:
算法导论