데이터 구조정렬02

1034 단어
힐 정렬
    /**
     *     
     * <p>1、       </p>
     * <p>2、       1(      )</p>
     */
    public void shellSort() {
        int[] arr = new int[]{3, 5, 9, 6, 7, 2, 4, 8, 0, 1};
System.out.println("Begin=>" + Arrays.toString(arr));
        int inner, outer;
        int temp;
        int h = 1;
        //    
        while (h <= arr.length / 3) {
            h = h * 3 + 1;
        }
        while (h > 0) {
            for (outer = h; outer < arr.length; outer ++) {
                temp = arr[outer];
                inner = outer;
                while (inner > h -1 && arr[inner - h] >= temp) {
                    arr[inner] = arr[inner - h];
                    inner -= h;
                    System.out.println(Arrays.toString(arr));
                }
                arr[inner] = temp;
            }
            //      
            h = (h - 1) / 3;
        }
System.out.println("End=>" + Arrays.toString(arr));
    }

좋은 웹페이지 즐겨찾기