page508.html

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

HTML
45
字号
<HTML><HEAD><TITLE>Two-Way Merge Sorting</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="tex2html7009" HREF="page509.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7007" HREF="page505.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7001" HREF="page507.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7011" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0015603000000000000000">Two-Way Merge Sorting</A></H3><P>Program&nbsp;<A HREF="page508.html#progtwoWayMergeSorterc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the code for the <tt>_sort</tt>and <tt>mergesort</tt> methods of the <tt>TwoWayMergeSorter</tt> class.The <tt>_sort</tt> method sets things up for therecursive <tt>mergesort</tt> method.First, it allocates a temporary array,the length of which is equal to the length of the array to be sorted (line&nbsp;4).Then it calls the recursive <tt>mergesort</tt> methodwhich sorts the array (line&nbsp;8).After the array has been sorted,the <tt>_sort</tt> method discards the temporary array (line&nbsp;6).<P><P><A NAME="44525">&#160;</A><A NAME="progtwoWayMergeSorterc">&#160;</A> <IMG WIDTH=575 HEIGHT=294 ALIGN=BOTTOM ALT="program44402" SRC="img2093.gif"  ><BR><STRONG>Program:</STRONG> <tt>TwoWayMergeSorter</tt> class 	<tt>_sort</tt> and <tt>mergesort</tt> methods.<BR><P><P>The <tt>mergesort</tt> method implements the recursive,divide-and-conquer merge sort algorithm described above.In addition to <tt>self</tt>,the <tt>mergesort</tt> method takes two parameters,<tt>left</tt> and <tt>right</tt>,that specify the subsequence of the array to be sorted.If the sequence to be sorted contains more than one element,the sequence is split in two (line&nbsp;10),each half is recursively sorted (lines&nbsp;11-12),and then two sorted halves are merged (line&nbsp;13).<P><HR><A NAME="tex2html7009" HREF="page509.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7007" HREF="page505.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7001" HREF="page507.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7011" 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 + -
显示快捷键?