page505.html

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

HTML
60
字号
<HTML><HEAD><TITLE>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="tex2html6972" HREF="page506.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6970" HREF="page478.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6964" HREF="page504.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6974" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION0015600000000000000000">Merge Sorting</A></H1><P>The fourth class of sorting algorithm we consider comprisesalgorithms that sort <em>by merging</em><A NAME=42627>&#160;</A><A NAME=42628>&#160;</A>.Merging is the combination of two or more sorted sequencesinto a single sorted sequence.<P>Figure&nbsp;<A HREF="page505.html#figmerge"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates the basic, two-way merge operation.In a two-way merge, two sorted sequences are merged into one.Clearly, two sorted sequences each of length <I>n</I>can be merged into a sorted sequence of length 2<I>n</I>in <I>O</I>(2<I>n</I>)=<I>O</I>(<I>n</I>) steps.However in order to do this,we need space in which to store the result.That is, it is not possible to merge the two sequences <em>in place</em>in <I>O</I>(<I>n</I>) steps.<P><P><A NAME="43799">&#160;</A><A NAME="figmerge">&#160;</A> <IMG WIDTH=575 HEIGHT=193 ALIGN=BOTTOM ALT="figure42631" SRC="img2083.gif"  ><BR><STRONG>Figure:</STRONG> Two-way merging.<BR><P><P>Sorting by merging is a recursive, divide-and-conquer strategy.In the base case, we have a sequence with exactly one element in it.Since such a sequence is already sorted, there is nothing to be done.To sort a sequence of <I>n</I><I>&gt;</I>1 elements:<OL><LI>	Divide the sequence into two sequences of length	 <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58273" SRC="img177.gif"  > and  <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline67953" SRC="img1774.gif"  >;<LI>	recursively sort each of the two subsequences; and then,<LI>	merge the sorted subsequences to obtain the final result.</OL>Figure&nbsp;<A HREF="page505.html#figsort7"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates the operationof the two-way merge sort algorithm.<P><P><A NAME="44326">&#160;</A><A NAME="figsort7">&#160;</A> <IMG WIDTH=575 HEIGHT=479 ALIGN=BOTTOM ALT="figure43805" SRC="img2084.gif"  ><BR><STRONG>Figure:</STRONG> Two-way merge sorting.<BR><P><BR> <HR><UL> <LI> <A NAME="tex2html6975" HREF="page506.html#SECTION0015601000000000000000">Implementation</A><LI> <A NAME="tex2html6976" HREF="page507.html#SECTION0015602000000000000000">Merging</A><LI> <A NAME="tex2html6977" HREF="page508.html#SECTION0015603000000000000000">Two-Way Merge Sorting</A><LI> <A NAME="tex2html6978" HREF="page509.html#SECTION0015604000000000000000">Running Time Analysis</A></UL><HR><A NAME="tex2html6972" HREF="page506.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6970" HREF="page478.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6964" HREF="page504.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6974" 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 + -
显示快捷键?