插入排序) 希爾排序 (最小增量排序)

/**
 * (插入排序) 希爾排序 (最小增量排序)
 * @author Cinn
 *
 */
public class shellSort {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] array= {48,58,50,98,69,51,27,99,100};
        shleesort(array);
        printArray(array);
    }
    
    /**
     * 希爾排序接口
     * @param array
     */
    public static void shleesort(int[] array){
        int temp = 0;
        int d = array.length;
        while(true){
            d = d/2;
            for(int x=0;x<d;x++){
                for (int i = x+d; i < array.length; i+=d) {
                    temp = array[i];
                    int j = i-d;
                    for(;j>=0&&temp<array[j];j-=d){
                        array[j+d] = array[j];
                    }
                    array[j+d] = temp;
                }
            }
            if(d==1){
                break;
            }
        }
    }

    public static void printArray(int[] array){
        for(int i=0;i<array.length;i++){   
            System.out.println(array[i]);   
        }   
    }
}


在本次希爾排序中,因爲   for(;j>=0&&temp<array[j];j-=d){寫成了成for(;j>0&&temp<array[j];j-=d){,導致元素中的第一個元素始終排序不到。糾結了半天。以後要注意這些低級錯誤。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章