Clarification on Sedgewick's “Algorithms” remark by the chapter “heapsort” (4th ed., Chapter 2.4)

The Book of Algorithms is currently being read . The Q&A section in chapter 2.4 in the implementation of heapsort based on priority queue (p. 328) has the following passage (let us focus on the heap of the priority queue rather than on heapsort):

Q. I still do not understand the purpose of the priority queues. Why don’t we sort, and then consider objects in ascending order of the sorted array?

a. In some data processing examples, such as TopM and Multiway, the total amount of data is too large to consider sorting (or even storing in memory). If you are looking for the top ten records among a billion elements, do you really want to sort an array with a billion records? From a priority queue, you can do this using a priority queue with ten inputs. In other examples, all the data does not even exist together at any point in time: we take something from the priority queue, process it, and the result of the processing will probably add a few more things to the priority of the queue.

TopM , Multiway are simple customer priority queues. The book talks about two phases of support:

  • heap building (the author uses a bunch of priority queues, we are interested)
  • sortdown

In my understanding, building a heap is almost sorting ("heap order"). To create a bunch, you practically need to visit each item in the source dataset.

Question: can anyone illustrate the author’s point, which I added to the bold in the above quote? How can we build a bunch without visiting all the items? What am I missing here? Cheers for clarinet.

+4
source share
1 answer

, . O (n). O (n log n). , , . . :

allocate priority queue q with space for t entries
visit each entry e in the input array
    queueIsFull := size(q) == t
    if !queueIsFull || e > min(q)
        if !queueIsFull                
            insert e into q
        else
            exchange min(q) with e and bubble up
 next

, , , . , O (log n), O (log t). O (n log n) O (n log t), log t log n.

+3

Source: https://habr.com/ru/post/1607281/


All Articles