java實現最大堆資料結構__java

來源:互聯網
上載者:User
package sort;import java.util.Random;public class HeapSort {    public static int SIZE = 10;    public static class Heap {        public int[] array;        public int size;        public Heap(int[] array) {            super();            this.array = array;            this.size = array.length - 1;        }        public int length() {            return array.length;        }        public void exchange(int i, int j) {            array[i] = array[i] ^ array[j];            array[j] = array[i] ^ array[j];            array[i] = array[i] ^ array[j];        }    }    //該方法的執行前提是:兩個子樹都是最大堆    public static void maxHeapify(Heap a, int i) {        int left = i << 1;        int right = left + 1;        int max = i;        if (left <= a.size && a.array[left] > a.array[i]) {            max = left;        }        if (right <= a.size && a.array[right] > a.array[max]) {            max = right;        }        if (max != i) {            a.exchange(max, i);            maxHeapify(a, max);        }    }    public static void buildMaxHeap(Heap a) {        for (int i = a.size >> 1; i > 0; i--) {            maxHeapify(a, i);        }    }    public static void heapSort(Heap a) {        buildMaxHeap(a);        int s = a.size;        for (int i = s; i >= 2; i--) {            a.exchange(i, 1);            a.size--;            maxHeapify(a, 1);        }    }    public static int heapMaxValue(Heap a) {        return a.array[1];    }    public static int poll(Heap a) throws Exception {        if (a.size == 0) {            throw new Exception("heap is empty!");        }        int max = a.array[1];        a.array[1] = a.array[a.size--];        maxHeapify(a, 1);        return max;    }    public static void heapIncreaseKey(Heap a, int i, int key) throws Exception {        if (a.array[i] > key) {            throw new Exception();        }        a.array[i] = key;        while (i > 1 && a.array[(i >> 1)] < key) {            a.exchange(i, i >> 1);            i = i >> 1;        }    }    public static void main(String[] args) {        int[] a = new int[SIZE];        Random random = new Random(100);        for (int i = 0; i < SIZE; i++) {            a[i] = (SIZE - i) * (SIZE - i);        }        Heap array = new Heap(a);        heapSort(array);        for (int i = 1; i < array.length(); i++) {            System.out.println(array.array[i]);        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.