Java Heap Sort code (minimum heap)

Source: Internet
Author: User


Package Com.juxuny.heap;import Java.lang.reflect.array;import Java.util.arraylist;import java.util.List;/** * Created by Juxuny on 2015/2/10.    */public class Heap {private int MAX = 2 << 20;    Private node[] arr;    private int size = 0;    Public Heap () {arr = new Node[max];            } public int Add (Node N) {if (size = = 0) {arr[size + +] = N;        return 0;        } if (size >= MAX) return-1;        Node tmp;        Arr[size + +] = N;        int j = size-1;                for (int i = (size-1) >> 1; I >= 0; i = (i-1) >> 1) {if (Arr[j].compare (Arr[i])) {                TMP = Arr[j];                ARR[J] = Arr[i];            Arr[i] = tmp;        } j = i;    } return 0;        } public Node Pop () {Node ret = null;        if (size = = 0) return ret;        ret = arr[0];        Node tmp = arr[--size];        int i = 0; for (i = 0; i < size >> 1;)         {   int child = i << 1 |            1;            int right = child + 1;            if (Right < size && Arr[right].compare (Arr[child]))            if (Tmp.compare (Arr[child])) break;            Arr[i] = Arr[child];        i = child;        } Arr[i] = tmp;    return ret;        Public Node Top () {if (size = = 0) return null;    return arr[0]; }}


Import Com.juxuny.heap.heap;import com.juxuny.heap.node;import java.util.random;/** * Created by Juxuny on 2015/2/10. */public class Main {public    static void Main (string[] args) {        random random = new random ();        Heap heap = new Heap ();        for (int i = 0; i <; i + +) {            Heap.add (new Minnode (Random.nextint (10000));        }        Minnode Minnode = (minnode) heap.pop ();        while (Minnode! = null) {            System.out.printf ("%d", minnode.time);            Minnode = (Minnode) heap.pop ();        }    }    Public  Static Class Minnode extends Node {public        long time;        Public Minnode (long time) {            this.time = time;        }        @Override Public        Boolean compare (Node N) {            return time < ((Minnode) n). Time;}}    }









Java Heap Sort code (minimum heap)

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.