page513.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 74 行
HTML
74 行
<HTML><HEAD><TITLE>Implementation</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="tex2html7063" HREF="page514.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7061" HREF="page512.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7057" HREF="page512.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7065" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0015811000000000000000">Implementation</A></H3><P>Program <A HREF="page513.html#progbucketSortera"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> introduces the <tt>BucketSorter</tt> class.The <tt>BucketSorter</tt> class extends the abstract <tt>Sorter</tt> classdefined in Program <A HREF="page481.html#progsortera"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.This bucker sorter is designed to sort specifically an array <tt>int</tt>s.The <tt>BucketSorter</tt> class contains two instance attributes,<tt>_m</tt> and <tt>_count</tt>.The integer <tt>_m</tt> simply keeps track of the size of the universe.The <tt>_count</tt> variable is an array of integersused to count the number of occurrences of each element of the universal set.<P><P><A NAME="45466"> </A><A NAME="progbucketSortera"> </A> <IMG WIDTH=575 HEIGHT=181 ALIGN=BOTTOM ALT="program45380" SRC="img2105.gif" ><BR><STRONG>Program:</STRONG> <tt>BucketSorter</tt> class <tt>__init__</tt> method.<BR><P><P>In addition to <tt>self</tt>,the <tt>__init__</tt> method for the <tt>BucketSorter</tt> classtakes a single argument which specifies the size of the universal set.The variable <tt>_m</tt> is set to the specified value,and the <tt>_count</tt> array is initialized to have the required size.<P>Program <A HREF="page513.html#progbucketSortera"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>_sort</tt> method.It begins by setting all of the counters to zero (lines 4-5).This can clearly be done in <I>O</I>(<I>m</I>) time.<P><P><A NAME="45469"> </A><A NAME="progbucketSorterb"> </A> <IMG WIDTH=575 HEIGHT=315 ALIGN=BOTTOM ALT="program45397" SRC="img2106.gif" ><BR><STRONG>Program:</STRONG> <tt>BucketSorter</tt> class <tt>_sort</tt> method.<BR><P><P>Next, a single pass is made through the datato count the number of occurrencesof each element of the universe (lines 6-7).Since each element of the array is examined exactly once,the running time is <I>O</I>(<I>n</I>).<P>In the final step, the sorted output sequence is created (lines 8-13).Since the output sequence contains exactly <I>n</I> items,the body of the inner loop (lines 10-13) is executed exactly <I>n</I> times.During the <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif" > iteration of the outer loop (lines 9-13),the loop termination test of the inner loop (line 10)is evaluated <IMG WIDTH=82 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline69789" SRC="img2107.gif" > times.As a result, the total running time of the final step is <I>O</I>(<I>m</I>+<I>n</I>).<P>Thus, the running time of the bucket sort method is <I>O</I>(<I>m</I>+<I>n</I>).Note that if <I>m</I>=<I>O</I>(<I>n</I>),the running time for bucket sort is <I>O</I>(<I>n</I>).That is, the bucket sort algorithm is a <em>linear-time</em> sorting algorithm!Bucket sort breaks the <IMG WIDTH=67 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60159" SRC="img527.gif" > bound associatedwith sorting algorithms that use binary comparisonsbecause bucket sort does not do any binary comparisons.The cost associated with breaking the <IMG WIDTH=67 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60159" SRC="img527.gif" > running time boundis the <I>O</I>(<I>m</I>) space required for the array of counters.Consequently, bucket sort is practical only for small <I>m</I>.For example, to sort 16-bit integersusing bucket sort requires the use of an array of <IMG WIDTH=85 HEIGHT=15 ALIGN=BOTTOM ALT="tex2html_wrap_inline69807" SRC="img2108.gif" > counters.<P><HR><A NAME="tex2html7063" HREF="page514.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7061" HREF="page512.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7057" HREF="page512.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7065" 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 + -
显示快捷键?