On algorithm and data structure five priority queues and heap sorting

Source: Internet
Author: User

In many applications, we usually need to deal with the processing object according to the priority situation, such as dealing with the highest priority object first, and then processing the object of the high level. The simplest example is when you play a game on a mobile phone, and if there is a call, then the system should prioritize incoming calls.

In this case, our data structure should provide two basic operations, one is to return the highest priority object, the other is to add a new object. This data structure is the priority queue (Priority queues).

This paper first introduces the definition of priority queue, ordered and unordered array and heap data structure to implement priority queue, and finally introduces the heap sort based on priority queue (Heap sort).

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

A definition

Priority queues are the same as the usual stacks and queues, except that each element in it has a "priority" that is processed first with the highest priority. If two elements have the same priority, they are processed in the order they are inserted into the queue.

Priority queues can be implemented through linked lists, arrays, heaps, or other data structures.

Two implementation

Array

The simplest priority queues can be implemented by an ordered or unordered array, and when the maximum value is obtained, the arrays are searched and returned. The code is simpler to implement, and it's not listed here.

As shown above:

· If you use a unordered array, you insert it directly at the end of the array at each insertion, and the time Complexity is O (1), but if you want to get the maximum value, or if the minimum value returns, you need to search for the time complexity of O (n).

· If you use an ordered array, each time you insert it, by inserting the sort to place the element in the correct position, the time complexity is O (n), but if you want to get the maximum value, the time complexity is O (1) because the Ashu is ordered and the element at the end of the array is returned directly.

Therefore, the use of ordinary array or linked list to achieve, can not make the insertion and sorting to achieve a better time complexity. So we need to adopt a new data structure to achieve. Here's how to use a binary heap (binary heap) to implement priority queues

Two-fork Heap

A binary heap is a structure of an approximate complete binary tree and satisfies the nature of the accumulation: the key value or index of a child node is always less than (or greater than) its parent. With this property, the maximum value on the two-fork heap is the root node.

Binary heap representation: We can use the index of the array to represent the position of the element in the binary heap.

From the two-fork heap, we can draw:

· The parent node of element K is located in [K/2]

· The location of the child nodes of element K is 2k and 2k+1

According to the rules above, we can use the index of two-dimensional arrays to represent binary heaps. Through the binary heap, we can realize the time complexity of inserting and deleting the maximum value to the O (Nlogn).

For a heap, the maximum element is already at the root node, and the deletion is to remove and return the root node element, at which point the binary heap needs to be rearranged; when inserting new elements, it is also necessary to rearrange the binary heap to satisfy the definition of the binary heap. Now let's look at both of these operations.

bottom-up Heap operation: If a node's value is greater than its parent's value, then the node needs to move up until it satisfies the node greater than its two subnodes, and less than its root node, thus achieving a two-fork heap for the entire heap.

As you can see from the figure above, we only need to compare the element k to its parent element K/2, if it is larger than the parent element, then swap, and then iterate, until it is smaller than the parent element.

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.