厨房小码农

Java排序算法之插入排序

package com.example.demo.dataStructure.sort;

// 直接插入排序
public class InsertSort {
    public static void insertSort(int[] arg) {
        for (int i=0;i<arg.length;i++) {
            int j = i;
            int temp = arg[j];
            while(j>0 && temp < arg[j-1]) {
                arg[j] = arg[j-1];
                j--;
            }
            arg[j] = temp;
        }
    }
    
    public static void main(String[] args) {
        int[] arg = {9,8,7,6,5,4,3,1,2};
        insertSort(arg);
        for (int i = 0;i< arg.length;i++) {
            System.out.print(arg[i]);
        }
    }
}

 

posted on 2018-09-12 23:44  厨房小码农  阅读(163)  评论(0编辑  收藏  举报

导航