public class DirectInsert{ public static void main(String[] args){ int[] array = {2,6,1,9,4,3,23,65,0,7}; System.out.print("the array before is:"); for(int i = 0; i < array.length; i++){ System.out.print(array[i]+" "); } System.out.println(" "); System.out.println("the array after is:"); directInsert(array); } private static void directInsert(int[] array){ int len = array.length; for(int i = 1; i < len; i++){ for(int j = i; j > 0; j--){ if(array[j] < array[j-1]){ int temp = array[j]; array[j] = array[j-1]; array[j-1] = temp; } } for(int z = 0; z < len; z++){ System.out.print(array[z]+" "); } System.out.println(" "); } } }
如图所示,为代码实现。