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

📄 page458.html

📁 wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq
💻 HTML
字号:
<HTML>
<HEAD>
<TITLE>Example-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="tex2html7580" HREF="page459.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page459.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="tex2html7578" HREF="page455.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page455.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="tex2html7572" HREF="page457.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page457.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="tex2html7582" 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="tex2html7583" 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>
<H2><A NAME="SECTION0015330000000000000000">Example-Merge Sorting</A></H2>
<A NAME="secalgsmergesort">&#160;</A>
<P>
Sorting algorithms and sorters are covered in detail in Chapter&nbsp;<A HREF="page483.html#chapsorting" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page483.html#chapsorting"><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>.
In this section we consider a divide-and-conquer sorting algorithm--<em>merge sort</em><A NAME=32954>&#160;</A>.
Given an array of <I>n</I> items in arbitrary order,
the objective is to rearrange the elements of the array
so that they are ordered from the smallest element to the largest one.
<P>
The merge sort algorithm sorts a sequence of length <I>n</I><I>&gt;</I>1
by splitting it into to subsequences--one 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"  >,
the other of length  <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"  >.
Each subsequence is sorted and then the two sorted sequences
are merged into one.
<P>
Program&nbsp;<A HREF="page458.html#progalgs3c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page458.html#progalgs3c"><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> defines the function <tt>MergeSort</tt> which
takes three arguments,
<tt>array</tt>, <tt>i</tt> and <tt>n</tt>.
The routine sorts the following <tt>n</tt> elements:
<P> <IMG WIDTH=450 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath68669" SRC="img1866.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1866.gif"  ><P>
The <tt>MergeSort</tt> routine calls itself
as well as the <tt>Merge</tt> routine.
The purpose of the <tt>Merge</tt> routine is to merge two sorted sequences,
one 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"  >,
the other of length  <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"  >,
into a single sorted sequence of length <I>n</I>.
This can easily be done in <I>O</I>(<I>n</I>) time.
(See Program&nbsp;<A HREF="page511.html#progsorter11c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page511.html#progsorter11c"><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>).
<P>
<P><A NAME="32977">&#160;</A><A NAME="progalgs3c">&#160;</A> <IMG WIDTH=575 HEIGHT=199 ALIGN=BOTTOM ALT="program32974" SRC="img1884.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1884.gif"  ><BR>
<STRONG>Program:</STRONG> Divide-and-Conquer Example--Merge Sorting<BR>
<P>
<P>
The running time of the <tt>MergeSort</tt> routine
depends on the number of items to be sorted, <I>n</I>.
Although Program&nbsp;<A HREF="page458.html#progalgs3c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page458.html#progalgs3c"><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> works correctly for arbitrary values of <I>n</I>,
it is much easier to determine the running time
if we assume that <I>n</I> is a power of two.
In this case,
the running time is given by the recurrence
<P><A NAME="eqnalgsms">&#160;</A> <IMG WIDTH=500 HEIGHT=48 ALIGN=BOTTOM ALT="equation32983" SRC="img1885.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1885.gif"  ><P>
<P>
Equation&nbsp;<A HREF="page458.html#eqnalgsms" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page458.html#eqnalgsms"><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> is easily solved using repeated substitution:
<P> <IMG WIDTH=500 HEIGHT=121 ALIGN=BOTTOM ALT="eqnarray32989" SRC="img1886.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1886.gif"  ><P>
Setting  <IMG WIDTH=61 HEIGHT=27 ALIGN=MIDDLE ALT="tex2html_wrap_inline68699" SRC="img1870.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1870.gif"  > gives  <IMG WIDTH=219 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68761" SRC="img1887.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1887.gif"  >.
<P>
<HR><A NAME="tex2html7580" HREF="page459.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page459.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="tex2html7578" HREF="page455.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page455.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="tex2html7572" HREF="page457.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page457.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="tex2html7582" 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="tex2html7583" 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -