page489.html

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

HTML
82
字号
<HTML><HEAD><TITLE>Bubble 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="tex2html6800" HREF="page490.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6798" HREF="page488.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6792" HREF="page488.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6802" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0015410000000000000000">Bubble Sort</A></H2><P>The simplest and, perhaps, the best known of the exchange sortsis the <em>bubble sort</em><A NAME=35168>&#160;</A>.<A NAME="tex2html895" HREF="footnode.html#35169"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/foot_motif.gif"></A>Figure&nbsp;<A HREF="page489.html#figsort2"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> shows the operation of bubble sort.<P><P><A NAME="36346">&#160;</A><A NAME="figsort2">&#160;</A> <IMG WIDTH=575 HEIGHT=546 ALIGN=BOTTOM ALT="figure35171" SRC="img2019.gif"  ><BR><STRONG>Figure:</STRONG> Bubble sorting.<BR><P><P>To sort the sequence  <IMG WIDTH=172 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline69245" SRC="img2020.gif"  >,bubble sort makes <I>n</I>-1 passes through the data.In each pass, adjacent elements are compared and swapped if necessary.First,  <IMG WIDTH=13 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline62095" SRC="img905.gif"  > and  <IMG WIDTH=11 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline62097" SRC="img906.gif"  > are compared;next,  <IMG WIDTH=11 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline62097" SRC="img906.gif"  > and  <IMG WIDTH=13 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline69255" SRC="img2021.gif"  >; and so on.<P>Notice that after the first pass through the data,the largest element in the sequence has <em>bubbled up</em>into the last array position.In general, after <I>k</I> passes through the data,the last <I>k</I> elements of the array are correctand need not be considered any longer.In this regard the bubble sort differs from the insertion sort algorithms--the sorted subsequence of <I>k</I> elements is never modified (by an insertion).<P>Figure&nbsp;<A HREF="page489.html#figsort2"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> also shows that while <I>n</I>-1 passes through the data are requiredto guarantee that the list is sorted in the end,it is possible for the list to become sorted much earlier!When no exchanges at all are made in a given pass,then the array is sorted and no additional passes are required.A minor algorithmic modificationwould be to count the exchanges made in a pass,and to terminate the sort when none are made.<P>Program&nbsp;<A HREF="page489.html#progbubbleSortera"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>BubbleSorter</tt> class.The <tt>BubbleSorter</tt> class extendsthe abstract <tt>Sorter</tt> classdefined in Program&nbsp;<A HREF="page481.html#progsortera"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.It simply provides an implementation for the <tt>_sort</tt> method.<P><P><A NAME="36506">&#160;</A><A NAME="progbubbleSortera">&#160;</A> <IMG WIDTH=575 HEIGHT=237 ALIGN=BOTTOM ALT="program36358" SRC="img2022.gif"  ><BR><STRONG>Program:</STRONG> <tt>BubbleSorter</tt> class <tt>__init__</tt> and <tt>_sort</tt> methods.<BR><P><P>The outer loop (lines&nbsp;8-12) is done for  <IMG WIDTH=195 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline69265" SRC="img2023.gif"  >.That makes <I>n</I>-1 iterations in total.During the  <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif"  > iteration of the outer loop,exactly <I>i</I>-1 iterations of the inner loop are done (lines&nbsp;9-11).Therefore, the number of iterations of the inner loop,summed over all the passes of the outer loop is<P> <IMG WIDTH=354 HEIGHT=46 ALIGN=BOTTOM ALT="displaymath69225" SRC="img2024.gif"  ><P>Consequently, the running time of bubble sort is  <IMG WIDTH=40 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline69273" SRC="img2025.gif"  >.<P>The body of the inner loop compares adjacent array elementsand swaps them if necessary (lines&nbsp;10-11).This takes at most a constant amount of time.Of course, the algorithm will run slightly faster when no swapping is needed.For example, this occurs if the array is already sorted to begin with.In the worst case,it is necessary to swap in every iteration of the inner loop.This occurs when the array is sorted initially in reverse order.Since only adjacent elements are swapped,bubble sort removes inversions one at time.Therefore, the average number of swaps required is  <IMG WIDTH=39 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline58629" SRC="img258.gif"  >.Nevertheless, the running time of bubble sort is always  <IMG WIDTH=40 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline69273" SRC="img2025.gif"  >.<P><HR><A NAME="tex2html6800" HREF="page490.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6798" HREF="page488.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6792" HREF="page488.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6802" 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 + -
显示快捷键?