page496.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 96 行

HTML
96
字号
<HTML><HEAD><TITLE>Selecting the Pivot</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="tex2html6874" HREF="page497.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6872" HREF="page488.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6868" HREF="page495.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6876" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0015450000000000000000">Selecting the Pivot</A></H2><P>The analysis in the preceding section showsthat selecting a good pivot is important.If we do a bad job of choosing the pivot,the running time of quicksort is  <IMG WIDTH=39 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline58629" SRC="img258.gif"  >.On the other hand,the average-case analysis shows that if every elementof a sequence is equally likely to be chosen for the pivot,the running time is  <IMG WIDTH=68 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59353" SRC="img402.gif"  >.This suggests that we can expect to get good performancesimply by selecting <em>a random pivot</em>!<P>If we expect to be sorting random input sequences,then we can achieve random pivot selection simply byalways choosing, say,the first element of the sequence to be the pivot.Clearly this can be done in constant time.(Remember, the analysis requires that  <IMG WIDTH=145 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline69391" SRC="img2046.gif"  >).As long as each element in the sequence is equally likely toappear in the first position,the average running time will be  <IMG WIDTH=68 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59353" SRC="img402.gif"  >.<P>In practice it is often the casethat the sequence to be sorted is almost sorted.In particular, consider what happens if the sequence to be sortedusing quicksort is already sorted.If we always choose the first element as the pivot,then we are guaranteed to have the worst-case running time!This is also true if we always pick the last element of the sequence.And it is also true if the sequence is initially sorted in reverse.<P>Therefore, we need to be more careful when choosing the pivot.Ideally, the pivot divides the input sequence exactly in two.That is, the ideal pivot is the <em>median</em><A NAME=37845>&#160;</A>element of the sequence.This suggests that the <tt>selectPivot</tt> method should find the median.To ensure that the running time analysis is valid,we need to find the median in <I>O</I>(<I>n</I>) time.<P>How do you find the median?One way is to sort the sequenceand then select the  <IMG WIDTH=49 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline69475" SRC="img2066.gif"  > element.But this is not possible,because we need to find the median to sort the sequence in the first place!<P>While it is possible to findthe median of a sequence of <I>n</I> elements in <I>O</I>(<I>n</I>) time,it is usually not necessary to do so.All that we really need to do is select a random element of the sequence while avoiding the problems described above.<P>A common way to do this is the<em>median-of-three pivot selection</em><A NAME=37849>&#160;</A>technique.In this approach,we choose as the pivot the median ofthe element at the left end of the sequence,the element at the right end of the sequence,and the element in the middle of the sequence.Clearly, this does the <em>right thing</em>if the input sequence is initially sorted(either in forward or reverse order).<P>Program&nbsp;<A HREF="page496.html#progmedianOfThreeQuickSortera"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> definesthe <tt>MedianOfThreeQuickSorter</tt> classThe <tt>MedianOfThreeQuickSorter</tt> classextends the abstract <tt>QuickSorter</tt> classintroduced in Program&nbsp;<A HREF="page491.html#progquickSortera"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.It provides an implementation for the <tt>selectPivot</tt> methodbased on median-of-three pivot selection.Notice that this algorithm does exactly three comparisonsto select the pivot.As a result, its running time is <I>O</I>(1).In practice this scheme performs sufficiently wellthat more complicated pivot selection approaches are unnecessary.<P><P><A NAME="38015">&#160;</A><A NAME="progmedianOfThreeQuickSortera">&#160;</A> <IMG WIDTH=575 HEIGHT=275 ALIGN=BOTTOM ALT="program37857" SRC="img2067.gif"  ><BR><STRONG>Program:</STRONG> <tt>MedianOfThreeQuickSorter</tt> class 	<tt>__init__</tt> and <tt>selectPivot</tt> methods.<BR><P><HR><A NAME="tex2html6874" HREF="page497.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6872" HREF="page488.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6868" HREF="page495.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6876" 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 &#169; 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 + -
显示快捷键?