page76.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 105 行
HTML
105 行
<HTML><HEAD><TITLE>Example-Bucket Sort</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="tex2html2083" HREF="page77.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2081" HREF="page72.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2075" HREF="page75.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2085" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION003440000000000000000">Example-Bucket Sort</A></H2><P>So far all of the asymptotic running time analyses presentedin this chapter have resulted in tight big oh bounds.In this section we consider an example which illustratesthat a cursory big oh analysis does not always result in a tight boundon the running time of the algorithm.<P>In this section we consider an algorithm to solve the following problem:Sort an array of <I>n</I> integers <IMG WIDTH=14 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline60025" SRC="img504.gif" >, <IMG WIDTH=14 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline60027" SRC="img505.gif" >, ..., <IMG WIDTH=31 HEIGHT=12 ALIGN=MIDDLE ALT="tex2html_wrap_inline60029" SRC="img506.gif" >,each of which is known to be between 0 and <I>m</I>-1 for some fixed <I>m</I>.An algorithm for solving this problem,called a <em>bucket sort</em><A NAME=2303> </A><A NAME=2304> </A>,is given in Program <A HREF="page76.html#progexamplen"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="2309"> </A><A NAME="progexamplen"> </A> <IMG WIDTH=575 HEIGHT=199 ALIGN=BOTTOM ALT="program2306" SRC="img507.gif" ><BR><STRONG>Program:</STRONG> Bucket sort.<BR><P><P>A bucket sort works as follows:An array of <I>m</I> counters, or <em>buckets</em><A NAME=2314> </A>, is used.Each of the counters is set initially to zero.Then, a pass is made through the input array,during which the buckets are used to keep a count of thenumber of occurrences of each value between 0 and <I>m</I>-1.Finally, the sorted result is produced by first placingthe required number of zeroes in the array,then the required number of ones, followed by the twos,and so on, up to <I>m</I>-1.<P>The analysis of the running time of Program <A HREF="page76.html#progexamplen"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>is summarized in Table <A HREF="page76.html#tblbucketsortc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Clearly, the worst-case running time of the first loop (lines 7-8) is <I>O</I>(<I>m</I>)and that of the second loop (lines 9-10) is <I>O</I>(<I>n</I>).<P><P><A NAME="2514"> </A><P> <A NAME="tblbucketsortc"> </A> <DIV ALIGN=CENTER><P ALIGN=CENTER><TABLE COLS=3 BORDER FRAME=HSIDES RULES=GROUPS><COL ALIGN=CENTER><COL ALIGN=CENTER><COL ALIGN=CENTER><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP COLSPAN=2> time</TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP><P> statement </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> cursory analysis </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> careful analysis </TD></TR></TBODY><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>2-3 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>O</I>(<I>m</I>) </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>O</I>(<I>m</I>) </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 4-5 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>O</I>(<I>n</I>) </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>O</I>(<I>n</I>) </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 6-10 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>O</I>(<I>mn</I>) </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>O</I>(<I>m</I>+<I>n</I>) </TD></TR></TBODY><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>TOTAL </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>O</I>(<I>mn</I>) </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>O</I>(<I>m</I>+<I>n</I>) </TD></TR></TBODY><CAPTION ALIGN=BOTTOM><STRONG>Table:</STRONG> Computing the running time of Program <A HREF="page76.html#progexamplen"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.</CAPTION></TABLE></P></DIV><P><P>Consider nested loops on lines 7-10.Exactly <I>m</I> iterations of the outer loop are done--the number of iterations of the outer loop is fixed.But the number of iterations of the inner loop dependson <code>bucket[j]</code>--the value of the counter.Since there are <I>n</I> numbers in the input array,in the worst case a counter may have the value <I>n</I>.Therefore, the running time of lines 7-10 is <I>O</I>(<I>mn</I>)and this running time dominates all the others,so the running time of Program <A HREF="page76.html#progexamplen"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is <I>O</I>(<I>mn</I>).(This is the <em>cursory analysis</em> column of Table <A HREF="page76.html#tblbucketsortc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).<P>Unfortunately, the cursory analysis has not produced a tight bound.To see why this is the case,we must consider the operation of Program <A HREF="page76.html#progexamplen"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> more carefully.In particular, since we are sorting <I>n</I> items,the final answer will only contain <I>n</I> items.Therefore, line 9 will be executed exactly <I>n</I> times--not <I>mn</I> times as the cursory result suggests.<P>Consider the inner loop at line 8.During the <IMG WIDTH=19 HEIGHT=30 ALIGN=MIDDLE ALT="tex2html_wrap_inline60083" SRC="img508.gif" > iteration of the outer loop,the inner loop does <IMG WIDTH=66 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60085" SRC="img509.gif" > iterations.According to Rule <A HREF="page73.html#ruleiii"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> this gives the worst-case running time of <IMG WIDTH=117 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60087" SRC="img510.gif" >.Therefore, the total running time is<P> <IMG WIDTH=500 HEIGHT=67 ALIGN=BOTTOM ALT="eqnarray2338" SRC="img511.gif" ><P>So, the running time of lines 6-10 is <I>O</I>(<I>m</I>+<I>n</I>)and therefore running time of Program <A HREF="page76.html#progexamplen"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is <I>O</I>(<I>m</I>+<I>n</I>).(This is the <em>careful analysis</em> column of Table <A HREF="page76.html#tblbucketsortc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).<P><HR><A NAME="tex2html2083" HREF="page77.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2081" HREF="page72.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2075" HREF="page75.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2085" 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 + -
显示快捷键?