直接插入排序
在要排序的一組數中,假設前面(n-1)[n>=2] 個數已經是排好順序的,現在要把第n個數插到前面的有序數中,使得這n個數也是排好順序的。
代碼實現:
public class InsertSort { public void insertSort() { int array[] = {54,23,52,76,35,25,87,64,43}; int temp = 0; for(int i=1;i<array.length;i++){ temp = array[i]; int j = i-1; for(;j>=0&&temp<array[j];j--){ array[j+1] = array[j]; } array[j+1] = temp; } for(int i=0;i<array.length;i++){ System.out.print(array[i]+" "); } }