⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page485.html

📁 Data Structures And Algorithms With Object-Oriented Design Patterns In Python (2003) source code and
💻 HTML
字号:
<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="tex2html6753" HREF="page486.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6751" HREF="page484.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6747" HREF="page484.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6755" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0015311000000000000000">Implementation</A></H3><P>Program&nbsp;<A HREF="page485.html#progstraightInsertionSortera"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the<tt>StraightInsertionSorter</tt> class.The <tt>StraightInsertionSorter</tt> 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="35295">&#160;</A><A NAME="progstraightInsertionSortera">&#160;</A> <IMG WIDTH=575 HEIGHT=221 ALIGN=BOTTOM ALT="program35080" SRC="img2002.gif"  ><BR><STRONG>Program:</STRONG> <tt>StraightInsertionSorter</tt> class 	<tt>__init__</tt> and <tt>_sort</tt> methods.<BR><P><P>In order to determine the running timeof the <tt>_sort</tt> method,we need to determine the number of iterations of the inner loop (lines&nbsp;9-11).The number of iterations of the inner loopin the  <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif"  > iteration of the outer loopdepends on the positions of the values in the array.In the best case, the value in position <I>i</I> of the arrayis larger than that in position <I>i</I>-1and zero iterations of the inner loop are done.In this case, the running time for insertion sort is <I>O</I>(<I>n</I>).Notice that the best case performance occurswhen we sort an array that is already sorted!<P>In the worst case, <I>i</I> iterations of the inner loop are requiredin the  <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif"  > iteration of the outer loop.This occurs when the value in position <I>i</I> of the array is smallerthan the values at positions 0 through <I>i</I>-1.Therefore, the worst case arises when we sort an array in which the elementsare initially sorted in reverse.In this case the running time for insertion sort is  <IMG WIDTH=39 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline58629" SRC="img258.gif"  >.<P><HR><A NAME="tex2html6753" HREF="page486.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6751" HREF="page484.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6747" HREF="page484.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6755" 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -