page504.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 68 行
HTML
68 行
<HTML><HEAD><TITLE>The Sorting Phase</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html6961" HREF="page505.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6959" HREF="page502.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6955" HREF="page503.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6963" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0015532000000000000000">The Sorting Phase</A></H3><P>Once the max heap has been built,heapsort proceeds to the selection sorting phase.In this phase the sorted sequence is obtainedby repeatedly withdrawing the largest element from the max heap.Figure <A HREF="page504.html#figsort6"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates how this is done.<P><P><A NAME="42605"> </A><A NAME="figsort6"> </A> <IMG WIDTH=575 HEIGHT=567 ALIGN=BOTTOM ALT="figure40894" SRC="img2081.gif" ><BR><STRONG>Figure:</STRONG> Heap sorting.<BR><P><P>The largest element of the heap is always found at the rootand the root of a complete tree is always in array position one.Suppose the heap occupies array positions 1 through <I>k</I>.When an element is withdrawn from the heap,its length decreases by one.That is, after the withdrawal the heap occupies array positions 1 through <I>k</I>-1.Thus, array position <I>k</I> is no longer required by the max heap.However, the next element of the sorted sequence belongs in position <I>k</I>!<P>So, the sorting phase of heapsort works like this:We repeatedly swap the largest element in the heap (always in position 1)into the next position of the sorted sequence.After each such swap,there is a new value at the root of the heap andthis new value is pushed down into the correct position in the heapusing the <tt>percolateDown</tt> method.<P>Program <A HREF="page504.html#progheapSorterc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the <tt>_sort</tt> methodof the <tt>HeapSorter</tt> class.The <tt>_sort</tt> method embodies both phases of the heapsort algorithm.In the first phase of heapsortthe <tt>buildHeap</tt> method is called to transformthe array into a max heap.As discussed above, this is done in <I>O</I>(<I>n</I>) time.<P><P><A NAME="42841"> </A><A NAME="progheapSorterc"> </A> <IMG WIDTH=575 HEIGHT=275 ALIGN=BOTTOM ALT="program42614" SRC="img2082.gif" ><BR><STRONG>Program:</STRONG> <tt>HeapSorter</tt> class <tt>_sort</tt> method.<BR><P><P>The second phase of the heapsort algorithm builds the sorted list.In all <I>n</I>-1 iterations of the loop on lines 8-11 are required.Each iteration involves one swapfollowed by a <tt>percolateDown</tt> operation.Since the worst-case running time for <tt>percolateDown</tt> is <IMG WIDTH=56 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59347" SRC="img400.gif" >,the total running time of the loop is <IMG WIDTH=68 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59353" SRC="img402.gif" >.The running time of the second phase asymptotically dominatesthat of the first phase.As a result, the worst-case running time of heapsort is <IMG WIDTH=68 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59353" SRC="img402.gif" >.<P><HR><A NAME="tex2html6961" HREF="page505.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6959" HREF="page502.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6955" HREF="page503.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6963" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?