Java implements maximum heap data structure __java

Source: Internet
Author: 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];
        The method is executed on the assumption that two subtrees are the largest heap 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));
 }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.