page509.html

来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 60 行

HTML
60
字号
<HTML>
<HEAD>
<TITLE>Merge Sorting</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
 <img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms 
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html8201" HREF="page510.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page510.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html8199" HREF="page483.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page483.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html8193" HREF="page508.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page508.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html8203" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html8204" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H1><A NAME="SECTION0016600000000000000000">Merge Sorting</A></H1>
<P>
The fourth class of sorting algorithm we consider comprises
algorithms that sort <em>by merging</em><A NAME=42838>&#160;</A><A NAME=42839>&#160;</A>.
Merging is the combination of two or more sorted sequences
into a single sorted sequence.
<P>
Figure&nbsp;<A HREF="page509.html#figmerge" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page509.html#figmerge"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/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.
I.e., 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="44010">&#160;</A><A NAME="figmerge">&#160;</A> <IMG WIDTH=575 HEIGHT=192 ALIGN=BOTTOM ALT="figure42842" SRC="img2199.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2199.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=36 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline58823" SRC="img180.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img180.gif"  > and  <IMG WIDTH=36 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline68743" SRC="img1883.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1883.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="page509.html#figsort7" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page509.html#figsort7"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> illustrates the operation
of the two-way merge sort algorithm.
<P>
<P><A NAME="44537">&#160;</A><A NAME="figsort7">&#160;</A> <IMG WIDTH=575 HEIGHT=479 ALIGN=BOTTOM ALT="figure44016" SRC="img2200.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2200.gif"  ><BR>
<STRONG>Figure:</STRONG> Two-Way Merge Sorting<BR>
<P><BR> <HR>
<UL> 
<LI> <A NAME="tex2html8205" HREF="page510.html#SECTION0016601000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page510.html#SECTION0016601000000000000000">Implementation</A>
<LI> <A NAME="tex2html8206" HREF="page511.html#SECTION0016602000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page511.html#SECTION0016602000000000000000">Merging</A>
<LI> <A NAME="tex2html8207" HREF="page512.html#SECTION0016603000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page512.html#SECTION0016603000000000000000">Two-Way Merge Sorting</A>
<LI> <A NAME="tex2html8208" HREF="page513.html#SECTION0016604000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page513.html#SECTION0016604000000000000000">Running Time Analysis</A>
</UL>
<HR><A NAME="tex2html8201" HREF="page510.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page510.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html8199" HREF="page483.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page483.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html8193" HREF="page508.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page508.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html8203" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html8204" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright &#169; 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.

</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?