堆排序(和堆實現的優先隊列一起 )

思路

取堆的一半后,分解最小子堆 (使用sink(),如果子結點有比父結點大的值的話 取較大子結點和父結點交換,滿足堆的性質),然后遞歸往上取父結點(父結點和其子結點使用sink(),使滿足堆的性質),這樣一直到根結點,這樣就構造了一個堆
交換根結點和最后一個結點,堆的規模-1,然后新的根結點下沉(重新滿足堆,根結點是當前堆的最大值)

注意

堆的第一個元素為空 N = this.a.length - 1;

public class HeapSort<K extends Comparable<K>> {
    public K[] a;
    //N 是去掉第一個元素之后  元素的個數
    int N ;
    public HeapSort (){
        
    }
    @SuppressWarnings("unchecked")
    public HeapSort (K[] a){
        this.a = (K[]) new Comparable[a.length + 1];
        for (int i = 0; i < a.length; i++) {
            this.a[i+1] = a[i];
        }
        N = this.a.length - 1;
    }
    public void sort(){
//      int N = a.length-1;
        for(int k = N/2; k > 0; k--){
            sink( k, N);
            
        }
        while(N > 0){
            System.out.println(N);
            exch( 1, N--);
            sink( 1, N);
        }
        System.out.println("");
    }

    private void exch(int i, int j) {
        K tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
    private boolean less(int j, int i) {
        
        return a[j].compareTo(a[i]) < 0 ? true : false;
    }
    private void sink(int k, int n) {
        while(2 * k <= n){
            int x = 2 * k;
            if(x < n && less(x, x + 1)){
                x++;
            }
            if(!less(k, x)){
                break;
            }
            exch(x, k);
            k = x;
        }
    }
}
    @Test
    public void test01(){
        Integer[] a = {5, 6, 2, 3, 1, 4};
        HeapSort<Integer> heapSort = new HeapSort<Integer>(a);
        heapSort.sort();
        for (int i = 0; i < 6; i++) {
        }
    
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容