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]);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/xiaobaobei/p/9638161.html