public class InsertSort {

    /**
     * 插入排序
     * @param
     * @return
     */
    public int[] sort(){
        int n[] = {8,9,7,0,2,3,5,6};
        int length = n.length;
        for(int i = 1; i< length; i++){
            int data = n[i]; // 暂存起来
            int j = i -1;
            for(;j >= 0; j--){
                if(n[j] > data){
                    n[j+1] = n[j];
                }else{
                    break;
                }
            }
            n[j+1] = data;
        }
        return n;
    }
}

实现起来很简单


Devilu
85 声望4 粉丝

just a newbie