白琳大佬带带我0003直接插入排序

吊毛你来啦:

public class InsertSort {
    
    
    
    public static void main(String[] args) {
    
    
        int[] array = new int[]{
    
    2, 6, 3, 8, 9, 0, 1, 7, 4};
        InsertSort(array);
    }
    
    public static void printArray(int[] array) {
    
    
        for(int i : array) {
    
    //遍历数组
            System.out.printf("%d\t", i);
        }
        System.out.println();
    }
    
    public static void InsertSort(int[] array) {
    
    
        if( array == null) {
    
    
            return;
        }
        
        for (int i = 1; i < array.length; i++) {
    
    
            if( array[i] < array[i - 1]) {
    
    
                int key = array[i];
                int j;
                for ( j = i - 1; j >= 0; j--) {
    
    
                    if ( key < array[j]) {
    
    
                        array[j + 1] = array[j];
                    } else {
    
    
                        break;
                    }
                }
                array[j + 1] = key;
            }
            printArray(array);//放在这给你妈的开开眼
        }
    }
}

运行结果

2	3	6	8	9	0	1	7	4	
0	2	3	6	8	9	1	7	4	
0	1	2	3	6	8	9	7	4	
0	1	2	3	6	7	8	9	4	
0	1	2	3	4	6	7	8	9	
//睡大觉了么么哒@广西大学黄毅然

猜你喜欢

转载自blog.csdn.net/qq_45864370/article/details/108905546