📄 page512.html
字号:
<HTML>
<HEAD>
<TITLE>Two-Way 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="tex2html8241" HREF="page513.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page513.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="tex2html8239" HREF="page509.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page509.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="tex2html8233" HREF="page511.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page511.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="tex2html8243" 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="tex2html8244" 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>
<H3><A NAME="SECTION0016603000000000000000">Two-Way Merge Sorting</A></H3>
<P>
Program <A HREF="page512.html#progsorter12c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page512.html#progsorter12c"><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> gives the code for the two <tt>DoSort</tt>
member functions of the <tt>TwoWayMergeSorter</tt> class.
The main <tt>DoSort</tt> routine takes as its lone argument
a reference to the array to be sorted (line 2).
First, it allocates a temporary array,
the length of which is equal to the length of the array to be sorted (line 4).
Then it calls the second, recursive <tt>DoSort</tt> routine
which sorts the array (line 5).
After the array has been sorted,
the main <tt>DoSort</tt> deletes the temporary array (line 6).
<P>
<P><A NAME="44738"> </A><A NAME="progsorter12c"> </A> <IMG WIDTH=575 HEIGHT=409 ALIGN=BOTTOM ALT="program44617" SRC="img2209.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2209.gif" ><BR>
<STRONG>Program:</STRONG> <tt>TwoWayMergeSorter<T></tt> Class <tt>DoSort</tt> Member Function Definitions<BR>
<P>
<P>
The second <tt>DoSort</tt> routine implements the recursive,
divide-and-conquer merge sort algorithm described above.
The routine takes three parameters--<tt>array</tt>,
<tt>left</tt> and <tt>right</tt>.
The first is a reference to the array to be sorted
and the latter two 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 16),
each half is recursively sorted (lines 17-18),
and then two sorted halves are merged (line 19).
<P>
<HR><A NAME="tex2html8241" HREF="page513.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page513.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="tex2html8239" HREF="page509.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page509.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="tex2html8233" HREF="page511.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page511.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="tex2html8243" 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="tex2html8244" 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 © 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -