直接插入排序

 1  2 
 3 public class MyInsertSort {
 4     public static void main(String[] args) {
 5         int num[] = {10, 8, 33, 54, 1, 6, 12, 43, 32, 27};
 6         insertSort(num);
 7         for (int i = 0; i < num.length; i++) {
 8             System.out.print(num[i] + " ");
 9         }
10     }
11 
12     private static void insertSort(int[] num) {
13         for (int i = 1; i < num.length; i++) {
14             int key = num[i];
15             int j = i - 1;
16             while (j>=0&&num[j]>key){
17                 num[j+1] = num[j];
18                 j--;
19             }
20             num[j+1] = key;
21         }
22     }
23 }

 

posted on 2020-07-22 20:47  含光Aries  阅读(143)  评论(0编辑  收藏  举报