博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java.util.TaskQueue的最小堆排序算法的应用
阅读量:5051 次
发布时间:2019-06-12

本文共 4997 字,大约阅读时间需要 16 分钟。

其中fixup和fixdown就是堆排序的使用。  

/** * This class represents a timer task queue: a priority queue of TimerTasks, * ordered on nextExecutionTime.  Each Timer object has one of these, which it * shares with its TimerThread.  Internally this class uses a heap, which * offers log(n) performance for the add, removeMin and rescheduleMin * operations, and constant time performance for the getMin operation. */class TaskQueue {    /**     * Priority queue represented as a balanced binary heap: the two children     * of queue[n] are queue[2*n] and queue[2*n+1].  The priority queue is     * ordered on the nextExecutionTime field: The TimerTask with the lowest     * nextExecutionTime is in queue[1] (assuming the queue is nonempty).  For     * each node n in the heap, and each descendant of n, d,     * n.nextExecutionTime <= d.nextExecutionTime.     */    private TimerTask[] queue = new TimerTask[128];    /**     * The number of tasks in the priority queue.  (The tasks are stored in     * queue[1] up to queue[size]).     */    private int size = 0;    /**     * Returns the number of tasks currently on the queue.     */    int size() {        return size;    }    /**     * Adds a new task to the priority queue.     */    void add(TimerTask task) {        // Grow backing store if necessary        if (size + 1 == queue.length)	    queue = Arrays.copyOf(queue, 2*queue.length);        queue[++size] = task;        fixUp(size);    }    /**     * Return the "head task" of the priority queue.  (The head task is an     * task with the lowest nextExecutionTime.)     */    TimerTask getMin() {        return queue[1];    }    /**     * Return the ith task in the priority queue, where i ranges from 1 (the     * head task, which is returned by getMin) to the number of tasks on the     * queue, inclusive.     */    TimerTask get(int i) {        return queue[i];    }    /**     * Remove the head task from the priority queue.     */    void removeMin() {        queue[1] = queue[size];        queue[size--] = null;  // Drop extra reference to prevent memory leak        fixDown(1);    }    /**     * Removes the ith element from queue without regard for maintaining     * the heap invariant.  Recall that queue is one-based, so     * 1 <= i <= size.     */    void quickRemove(int i) {        assert i <= size;        queue[i] = queue[size];        queue[size--] = null;  // Drop extra ref to prevent memory leak    }    /**     * Sets the nextExecutionTime associated with the head task to the     * specified value, and adjusts priority queue accordingly.     */    void rescheduleMin(long newTime) {        queue[1].nextExecutionTime = newTime;        fixDown(1);    }    /**     * Returns true if the priority queue contains no elements.     */    boolean isEmpty() {        return size==0;    }    /**     * Removes all elements from the priority queue.     */    void clear() {        // Null out task references to prevent memory leak        for (int i=1; i<=size; i++)            queue[i] = null;        size = 0;    }    /**     * Establishes the heap invariant (described above) assuming the heap     * satisfies the invariant except possibly for the leaf-node indexed by k     * (which may have a nextExecutionTime less than its parent's).     *     * This method functions by "promoting" queue[k] up the hierarchy     * (by swapping it with its parent) repeatedly until queue[k]'s     * nextExecutionTime is greater than or equal to that of its parent.     */    private void fixUp(int k) {        while (k > 1) {            int j = k >> 1;            if (queue[j].nextExecutionTime <= queue[k].nextExecutionTime)                break;            TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;            k = j;        }    }    /**     * Establishes the heap invariant (described above) in the subtree     * rooted at k, which is assumed to satisfy the heap invariant except     * possibly for node k itself (which may have a nextExecutionTime greater     * than its children's).     *     * This method functions by "demoting" queue[k] down the hierarchy     * (by swapping it with its smaller child) repeatedly until queue[k]'s     * nextExecutionTime is less than or equal to those of its children.     */    private void fixDown(int k) {        int j;        while ((j = k << 1) <= size && j > 0) {            if (j < size &&                queue[j].nextExecutionTime > queue[j+1].nextExecutionTime)                j++; // j indexes smallest kid            if (queue[k].nextExecutionTime <= queue[j].nextExecutionTime)                break;            TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;            k = j;        }    }    /**     * Establishes the heap invariant (described above) in the entire tree,     * assuming nothing about the order of the elements prior to the call.     */    void heapify() {        for (int i = size/2; i >= 1; i--)            fixDown(i);    }}

转载于:https://www.cnblogs.com/zhwj184/archive/2012/05/31/3027499.html

你可能感兴趣的文章
.net 写文件上传下载webservice
查看>>
noSQL数据库相关软件介绍(大数据存储时候,必须使用)
查看>>
iOS开发——缩放图片
查看>>
HTTP之URL的快捷方式
查看>>
满世界都是图论
查看>>
配置链路聚合中极小错误——失之毫厘谬以千里
查看>>
【BZOJ4487】[JSOI2015] 染色问题(高维容斥)
查看>>
Ubuntu 环境变量
查看>>
一步一步学MySQL-日志文件
查看>>
bzoj3994: [SDOI2015]约数个数和
查看>>
hdu5306 Gorgeous Sequence
查看>>
Android中使用ListView实现下拉刷新和上拉加载功能
查看>>
proc文件系统的简介
查看>>
连续自然数和
查看>>
[SDOI2015]星际战争
查看>>
用好lua+unity,让性能飞起来——luajit集成篇/平台相关篇
查看>>
JS控制页面跳转
查看>>
Ubuntu PPA软件源
查看>>
Window 2003 IIS + MySQL + PHP + Zend 环境配置
查看>>
Mysql集合笔记
查看>>