A Priority Queue: The Binary Heap

Draw ANSI/AIM Code 39 In JavaUsing Barcode encoder for Java Control to generate, create Code39 image in Java applications.

Figure 2141 Example of run construction

Barcode Encoder In JavaUsing Barcode generation for Java Control to generate, create barcode image in Java applications.

If we are clever, we can make the length of the runs that we initially construct larger than the amount of available main memoryThis technique is called replacement selection

Decode Bar Code In JavaUsing Barcode recognizer for Java Control to read, scan read, scan image in Java applications.

Using this observation, we can write an algorithm for producing runs, commonly called replacement selection Initially, M elements are read into memory and placed in a priority queue efficiently with a single buildHeap We perform a deleteMin,writing the smallest element to the output tape We read the next element from the input tape If it is larger than the element just written, we can add it to the priority queue with an insert;otherwise, it cannot go into the current run Because the priority queue is smaller by one element, this element is stored in the dead space of the priority queue until the run has been completed and is then used for the next run Storing an element in the dead space is exactly what is done in heapsort We continue doing this process until the size of the priority queue is 0, at which point the run is over We start a new run by rebuilding a new priority queue with a buildHeap operation, in the process using all of the elements in the dead space Figure 2141 shows the run construction for the small example we have been using, with M = 3 Elements that are reserved for the next run are shaded Elements 1 1,94, and 81 are placed with buildHeapElement 1 I is output, and then 96 is placed in the heap by an insertion because it is larger

Code 39 Full ASCII Generator In C#.NETUsing Barcode encoder for Visual Studio .NET Control to generate, create Code 3/9 image in .NET applications.

Objects of the Game

Code39 Drawer In VS .NETUsing Barcode maker for ASP.NET Control to generate, create Code 3/9 image in ASP.NET applications.

than 1 1 Element 8 1 is output next, and then 12 is read As 12 is smaller than the 82 just output, it cannot be included in the current run Thus it is placed in the heap dead space The heap now logically contains only 94 and 96 After they are output, we have only dead space elements, so we construct a heap and begin run 2 In this example, replacement selection produces only 3 runs, compared to the 5 runs obtained by sorting As a result, a three-way merge finishes in one pass instead of two If the input is randomly distributed, replacement selection produces runs of average length 2M For our large example, we would expect 160 runs instead of 320 runs, so a five-way merge would still require four passes In this case, we have not saved a pass, although we might if we get lucky and have 125 runs or fewer Because external sorts take so long, every pass saved can make a significant difference in the running time As we have shown, replacement selection may do no better than the standard algorithm However, the input is frequently nearly sorted to start with, in which case replacement selection produces only a few abnormally long runs This kind of input is common for external sorts and makes replacement selection extremely valuable

USS Code 39 Creation In .NETUsing Barcode creator for .NET Control to generate, create Code 39 image in .NET applications.

Summary

Printing Code39 In Visual Basic .NETUsing Barcode printer for .NET Control to generate, create Code 39 Extended image in VS .NET applications.

In this chapter we showed an elegant implementation of the priority queue The binary heap uses only an array, yet it supports the basic operations in logarithmic worst-case time The heap leads to a popular sorting algorithm, heapsort In Exercises 2 1 -26 and 2 127 you are asked to compare the performance of heapsort with that of quicksort Generally speaking, heapsort is slower than quicksort but it is certainly easier to implement Finally, we showed that priority queues are important data structures for external sorting This completes implementation of the fundamental and classic data structures In Part V we examine more sophisticated data structures, beginning with the splay tree, a binary search tree that has some remarkable properties

EAN / UCC - 14 Maker In JavaUsing Barcode drawer for Java Control to generate, create EAN128 image in Java applications.

Draw Bar Code In JavaUsing Barcode maker for Java Control to generate, create bar code image in Java applications.

Generating UPC - 13 In JavaUsing Barcode maker for Java Control to generate, create EAN13 image in Java applications.

MSI Plessey Drawer In JavaUsing Barcode creation for Java Control to generate, create MSI Plessey image in Java applications.

Painting DataMatrix In VB.NETUsing Barcode encoder for VS .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications.

Creating ANSI/AIM Code 39 In VB.NETUsing Barcode encoder for VS .NET Control to generate, create Code 39 Extended image in VS .NET applications.

Generating Bar Code In Visual Basic .NETUsing Barcode generator for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.