Algorithm (4th edition)-2.4 priority queue

Source: Internet
Author: User
Tags comparable

Definition: A data structure that supports deleting the largest element and inserting elements.

Classic implementation: Based on binary heap data structure.

2.4.1 API

1. As long as we are able to efficiently implement insert () and Delmin (), the MINPQ topm in the following priority queue use case can be used to solve this problem using the priority queue.

2.4.2 Primary Implementation

1. Array implementation (unordered): modify Pop (), first exchange and then delete, the equivalent of the choice of sorting (personally think). Lazy Method

2. Array implementation (ordered): modify Insert (), the maximum value is guaranteed at the top of the stack after each insert. Positive Methods

3. List notation: based on the code of the lower stack of the linked list, select one of the above implementations.

4. The various implementations of the priority queue increase the order of magnitude in the worst-case run time

Data inserting elements Delete the largest element
Ordered array N 1
Unordered array 1 N
Heap Logn Logn
Ideal situation 1 1

Definition of 2.4.3 Heap

1. Heap Order: Each node of a binary tree is greater than or equal to its two sub-nodes.

2. The root node is the largest junction in a heap-ordered two-fork tree.

3. The two-fork heap is a set of elements that can be sorted by a full binary tree in a heap order and stored hierarchically in the array (the first position of the array is not used).

4.* in a heap, the parent node of the node of location K is K/2, and its two sub-nodes are located at 2k and 2k + 1 respectively.

5. The height of a complete binary tree of size n is LgN?

Algorithm of 2.4.4 Heap

1. Understand floating (swim) and sinking (sink).

2. Insert the element.

We add the new element to the end of the array, increase the size of the heap and let the new element float to the right place.

3. Delete the largest element.

We delete the largest element from the top of the array and put the last element of the array to the top, reducing the size of the heap and letting the element sink to the appropriate position.

4. For a heap-based priority queue with n elements, the Insert element operation requires no more than (LgN + 1) Comparisons, and the operation to delete the largest element takes no more than 2lgN comparisons.

2.4.5 Heap Sorting

 Public classHeap { Public Static voidsort (comparable[] a) {intN =a.length;  for(intK = N/2; K >= 1; k--) Sink (A, k, N);  while(N > 1) {Exch (A,1, n--); y Sink (A,1, N); }    }  Private Static BooleanLess (comparable[] PQ,intIintj) {returnPq[i-1].compareto (Pq[j-1]) < 0; }    Private Static BooleanLess (comparable V, comparable W) {returnV.compareto (W) < 0; }  Private Static voidExch (object[] PQ,intIintj) {Object swap= Pq[i-1]; Pq[i-1] = pq[j-1]; Pq[j-1] =swap; }  Private Static voidSink (comparable[] PQ,intKintN) { while(2*k <=N) {intj =K; if(J < n && Less (PQ, J, J+1) J + +; if(!less (PQ, K, J)) Break;      Exch (PQ, K, J); K=J; }  }     Public Static Booleanissorted (comparable[] a) {//test array elements for order         for(inti = 1; i < a.length; i++)            if(Less (A[i], a[i-1]))return false; return true; }    Private Static voidShow (comparable[] a) {//to print an array in a single line         for(inti = 0; i < a.length; i++) Stdout.print (A[i]+ " ");    Stdout.println (); }     Public Static voidMain (string[] args) {//reads strings from standard input, sorts them, and outputsString[] A =in.readstrings ();        Sort (a); assertissorted (a);    Show (a); }}
Heap

1. With sinking operations, the heap is constructed from n elements with fewer than 2N comparisons and less than n interchanges.

2. Heap sequencing plays an important role in the study of sequencing complexity, because it is the only way we know to be able to use space and time optimally simultaneously-in the worst case, it also guarantees the use of ~2NLGN comparisons and constant extra space. When space is tight (for example, in embedded systems or in low-cost mobile devices) It is popular because he can achieve (and even machine code) better performance in just a few lines.

3. However, many applications of modern systems seldom use it because it cannot take advantage of caching. Array elements are rarely compared to other adjacent elements, so the number of cache misses is much higher than most comparisons are made between adjacent elements, such as quick sort, merge sort, or even hill sort.

Algorithm (4th edition)-2.4 priority queue

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.