﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Mr.Johnson</title><link>http://www.cppblog.com/ylxin1993/</link><description /><language>zh-cn</language><lastBuildDate>Fri, 17 Apr 2026 02:12:56 GMT</lastBuildDate><pubDate>Fri, 17 Apr 2026 02:12:56 GMT</pubDate><ttl>60</ttl><item><title>Code Style Conventions</title><link>http://www.cppblog.com/ylxin1993/archive/2013/02/10/197789.html</link><dc:creator>Johnson Yuan</dc:creator><author>Johnson Yuan</author><pubDate>Sat, 09 Feb 2013 16:29:00 GMT</pubDate><guid>http://www.cppblog.com/ylxin1993/archive/2013/02/10/197789.html</guid><wfw:comment>http://www.cppblog.com/ylxin1993/comments/197789.html</wfw:comment><comments>http://www.cppblog.com/ylxin1993/archive/2013/02/10/197789.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ylxin1993/comments/commentRss/197789.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ylxin1993/services/trackbacks/197789.html</trackback:ping><description><![CDATA[<div><p><strong>GENERAL</strong></p><p><strong>-------</strong></p><p><br /></p><p><strong>Use real tabs that equal 4 spaces.</strong></p><p><br /></p><p><br /></p><p><strong>Use typically trailing braces everywhere (if,else, functions, structures, typedefs, class definitions, etc.)</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">if ( x ) {</p><p style="font-weight:normal">}</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p><strong>The else statement starts on the same line asthe last closing brace.</strong></p><p><br /></p><p style="font-weight:normal">if ( x ) {</p><p style="font-weight:normal">} else {<br />}</p><p style="font-weight:normal"><br /></p><p><strong>Pad parenthesized expressions with spaces</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">if ( x ) {</p><p style="font-weight:normal">}</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">Instead of </p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">if (x) {</p><p style="font-weight:normal">}</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">And</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">x = ( y * 0.5f );</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">Instead of</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">x = (y * 0.5f);</p><p style="font-weight:normal"><br /></p><p><strong>Use precision specification for floating pointvalues unless there is an explicit need for a double.</strong></p><p><br /></p><p style="font-weight:normal">float f = 0.5f;</p><p><br /></p><p><strong>Instead of</strong></p><p><br /></p><p style="font-weight:normal">float f = 0.5;</p><p style="font-weight:normal"><br /></p><h2>And</h2><p><br /></p><p style="font-weight:normal">float f = 1.0f;</p><p><br /></p><p><strong>Instead of</strong></p><p><br /></p><p style="font-weight:normal">float f = 1.f;</p><p><br /></p><p style="font-weight:normal"><br /></p><p><strong>Function names start with an upper case:</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">void Function( void );</p><p style="font-weight:normal"><br /></p><p><br /></p><p><strong>In multi-word function names each word startswith an upper case:</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">voidThisFunctionDoesSomething( void );</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p><strong>The standard header for functions is:</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">/*</p><p style="font-weight:normal">====================</p><p style="font-weight:normal">FunctionName</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">  Description</p><p style="font-weight:normal">====================</p><p style="font-weight:normal">*/</p><p style="font-weight:normal"><br /></p><p><br /></p><p><strong>Variable names start with a lower casecharacter.</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">float x;</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p><strong>In multi-word variable names the first wordstarts with a lower case character and each successive word startswith an upper case.</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">floatmaxDistanceFromPlane;</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><h1><strong>Typedef names use the same naming convention as variables, howeverthey always end with "_t".</strong></h1><p style="font-weight:normal"><br /></p><p style="font-weight:normal">typedef intfileHandle_t;</p><h1><strong>Struct names use the same naming convention as variables, howeverthey always end with "_t".</strong></h1><p style="font-weight:normal"><br /></p><p style="font-weight:normal">struct renderEntity_t;</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p><strong>Enum  names use the same naming convention asvariables, however they always  end with  "_t". The enumconstants use all upper case characters.  Multiple words are separatedwith an underscore.</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">enum contact_t {</p><p style="font-weight:normal">	CONTACT_NONE,</p><p style="font-weight:normal">	CONTACT_EDGE,</p><p style="font-weight:normal">	CONTACT_MODELVERTEX,</p><p style="font-weight:normal">	CONTACT_TRMVERTEX</p><p style="font-weight:normal">};</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><h1><strong>Names of recursive functions end with "_r"</strong></h1><p style="font-weight:normal"><br /></p><p style="font-weight:normal">void WalkBSP_r( intnode );</p><p style="font-weight:normal"><br /></p><p><br /></p><p><strong>Defined names use all upper case characters.Multiple words are separated with an underscore.</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">#define SIDE_FRONT		0</p><p style="font-weight:normal"><br /></p><p><br /></p><p><strong>Use &#8216;const&#8217; as much as possible.</strong></p><p><br /></p><p><strong>Use:</strong></p><p><br /></p><p style="font-weight:normal">const int *p;			//pointer to const int</p><p style="font-weight:normal">int * const p;			//const pointer to int</p><p style="font-weight:normal">const int * constp;	// const pointer to const int</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><strong>Don&#8217;t use</strong>:</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">int const *p;</p><p><br /></p><p><br /></p><p><strong>CLASSES</strong></p><p><strong>-------</strong></p><p style="font-weight:normal"><br /></p><h1><strong>The standard header for a class is:</strong></h1><p style="font-weight:normal"><br /></p><p style="font-weight:normal;widows:2;orphans:2">/*</p><p style="font-weight:normal;widows:2;orphans:2">===============================================================================</p><p style="font-weight:normal;widows:2;orphans:2"><br /></p><p style="font-weight:normal;widows:2;orphans:2">	Description</p><p style="font-weight:normal;widows:2;orphans:2"><br /></p><p style="font-weight:normal;widows:2;orphans:2">===============================================================================</p><p style="font-weight:normal;widows:2;orphans:2">*/</p><p style="font-weight:normal"><br /></p><h1><strong>Class names start with "id" and each successive wordstarts with an upper case.</strong></h1><p style="font-weight:normal"><br /></p><p style="font-weight:normal">class idVec3;</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p><strong>Class variables have the same naming conventionas variables.</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">class idVec3 {</p><p style="font-weight:normal">	float		x;</p><p style="text-indent:0.5in;font-weight:normal">float		y;</p><p style="text-indent:0.5in;font-weight:normal">float		z;</p><p style="font-weight:normal">}</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p><strong>Class methods have the same naming conventionas functions.</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">class idVec3 {</p><p style="font-weight:normal">	float		Length( void )const;</p><p style="font-weight:normal">}</p><p style="font-weight:normal"><br /></p><p><strong>Indent  the names of class variables and classmethods to make nice columns. The  variable type or method return typeis in the first column and the  variable name or method name is in thesecond column.</strong></p><p><br /></p><p style="font-weight:normal">class idVec3 {</p><p style="font-weight:normal">	float		x;</p><p style="text-indent:0.5in;font-weight:normal">float		y;</p><p style="text-indent:0.5in;font-weight:normal">float		z;</p><p style="font-weight:normal">	float		Length( void )const;</p><p style="font-weight:normal">	const float*	ToFloatPtr( void ) const;</p><p style="font-weight:normal">}</p><p style="font-weight:normal"><br /></p><p><strong>The * of the pointer is in the first columnbecause it improves readability when considered part of the type.</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p><strong>Ording of class variables and methods should beas follows:</strong></p><p><br /></p><ol><li><p style="font-weight:normal">list of friend	classes</p>	</li><li><p style="font-weight:normal">public variables</p>	</li><li><p style="font-weight:normal">public methods</p>	</li><li><p style="font-weight:normal">protected	variables</p>	</li><li><p style="font-weight:normal">protected methods</p>	</li><li><p style="font-weight:normal">private variables</p>	</li><li><p style="font-weight:normal">private methods</p></li></ol><p style="font-weight:normal"><br /></p><p style="font-weight:normal">This allows the publicinterface to be easily found at the beginning of the class.</p><p style="font-weight:normal"><br /></p><p><br /></p><p><strong>Always make class methods &#8216;const&#8217; when theydo not modify any class variables.</strong></p><p><br /></p><p><strong>Avoid  use of &#8216;const_cast&#8217;.  When object isneeded to be modified, but only  const versions are accessible, createa function that clearly gives an  editable version of the object. This keeps the control of the  &#8216;const-ness&#8217; in the hands of theobject and not the user.</strong></p><p><br /></p><p><strong>Return  &#8216;const&#8217; objects unless the generalusage of the object is to change its  state.  For example, mediaobjects like idDecls should be const to a  majority of the code, whileidEntity objects tend to have their state  modified by a variety ofsystems, and so are ok to leave</strong></p><p><strong>non-const.</strong></p><p><br /></p><p><strong>Function overloading should be avoided in mostcases.  For example, instead of:</strong></p><p><br /></p><p style="font-weight:normal">	const idAnim*	GetAnim( int index ) const;</p><p style="font-weight:normal">	const idAnim*	GetAnim( const char *name ) const;</p><p style="font-weight:normal">	const idAnim*	GetAnim( float randomDiversity ) const;</p><p style="font-weight:normal"><br /></p><p><strong>Use:</strong></p><p><br /></p><p style="font-weight:normal">	const idAnim*	GetAnimByIndex( int index ) const;</p><p style="font-weight:normal">	const idAnim*	GetAnimByName( const char *name ) const;</p><p style="font-weight:normal">	const idAnim*	GetRandomAnim( float randomDiversity ) const;</p><p><br /></p><p><strong>Explicitly  named functions tend to be lessprone to programmer error and  inadvertent calls to functions due towrong data types being passed in as  arguments.  Example:</strong></p><p><br /></p><p style="font-weight:normal">Anim = GetAnim( 0 );</p><p><strong><br />This could be meant as a call to get arandom animation, but the compiler would interpret it as a call toget one by index.</strong></p><p><br /></p><p><strong>Overloading functions for the sake of adding&#8216;const&#8217; accessible function is allowable:</strong></p><p><br /></p><p style="font-weight:normal">class idAnimatedEntity: public idEntity {</p><p style="text-indent:0.5in;font-weight:normal">idAnimator* 		GetAnimator( void );</p><p style="text-indent:0.5in;font-weight:normal">constidAnimator * 	GetAnimator( void ) const;</p><p style="font-weight:normal">};</p><p style="font-weight:normal"><br /></p><p><strong>In  this case, a const version of GetAnimatorwas provided in order to allow  GetAnimator to be called from constfunctions.  Since idAnimatedEntity  is normally a non-const object,this is allowable.  For a media type,  which is normally const,operator overloading should be avoided:</strong></p><p style="font-weight:normal"><strong><br /></strong>classidDeclMD5 : public idDecl {</p><p style="text-indent:0.5in;font-weight:normal">constidMD5Anim *	GetAnim( animHandle_t handle ) const;</p><p style="text-indent:0.5in;font-weight:normal">idMD5Anim*		GetEditableAnim( animHandle_t handle );</p><p style="font-weight:normal">};</p><p><br /></p><p><strong>id Studio Names</strong></p><p><strong>---------------</strong></p><p style="font-weight:normal"><br /></p><p style="margin-right:0in;font-weight:normal;widows:2;orphans:2">id&lt;name&gt;Dlg&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;//&nbsp;dialog class</p><p style="margin-right:0in;font-weight:normal">id&lt;name&gt;Ctrl&nbsp;&nbsp;&nbsp;&nbsp;// dialog control class</p><p style="margin-right:0in;font-weight:normal">id&lt;name&gt;Frm&nbsp;&nbsp;&nbsp;&nbsp; // frame window</p><p style="margin-right:0in;font-weight:normal">id&lt;name&gt;View&nbsp;&nbsp;&nbsp; // view window</p><p style="margin-right:0in;font-weight:normal">id&lt;name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//any other class</p><p><br /></p><p><br /></p><p><strong>FILE NAMES</strong></p><p><strong>---------</strong></p><p style="font-weight:normal"><br /></p><p>Each class should be in a seperate source fileunless it makes sense to group several smaller classes.</p><p><strong>The file name should be the same as the name ofthe class without the "id" prefix. (Upper/lower case ispreserved.)</strong></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">class idWinding;</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">files:</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">Winding.cpp</p><p style="font-weight:normal">Winding.h</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p>When  a class spans across multiple files thesefiles have a name that starts  with the name of the class without"id", followed by an underscore and a  subsection name.</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">class idRenderWorld;</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">files:</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">RenderWorld_load.cpp</p><p style="font-weight:normal">RenderWorld_demo.cpp</p><p style="font-weight:normal">RenderWorld_portals.cpp</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p>When  a class is a public virtual interface to asubsystem the public  interface is implemented in a header file withthe name of the class  without "id". The definition of theclass that implements the subsystem  is placed in a header file withthe name of the class without "id" and  ends with"_local.h". The implementation of the subsystem is placedin a  cpp file with the name of the class without "id".</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">class idRenderWorld;</p><p style="font-weight:normal"><br /></p><p style="font-weight:normal">RenderWorld.h			//public virtual idRenderWorld interface</p><p style="font-weight:normal">RenderWorld_local.h		//definition of class idRenderWorldLocal</p><p style="font-weight:normal">RenderWorld.cpp		//implementation of idRenderWorldLocal</p></div> <img src ="http://www.cppblog.com/ylxin1993/aggbug/197789.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ylxin1993/" target="_blank">Johnson Yuan</a> 2013-02-10 00:29 <a href="http://www.cppblog.com/ylxin1993/archive/2013/02/10/197789.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Binary Search, Buttle Sort, Insertion Sort(include Big O Notation)</title><link>http://www.cppblog.com/ylxin1993/archive/2012/12/24/196577.html</link><dc:creator>Johnson Yuan</dc:creator><author>Johnson Yuan</author><pubDate>Mon, 24 Dec 2012 12:18:00 GMT</pubDate><guid>http://www.cppblog.com/ylxin1993/archive/2012/12/24/196577.html</guid><wfw:comment>http://www.cppblog.com/ylxin1993/comments/196577.html</wfw:comment><comments>http://www.cppblog.com/ylxin1993/archive/2012/12/24/196577.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ylxin1993/comments/commentRss/196577.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ylxin1993/services/trackbacks/196577.html</trackback:ping><description><![CDATA[<h2>Hour3 Ordered Array</h2>  <p><strong>Binary Search</strong> (Sorted array):</p>  <p>Given the range, we want to know <strong>how many comparison</strong>s it will take to complete a search.</p>  <p>a binary search provides a significant speed increase over a linear search. &nbsp;For very small numbers of items, the difference isn&#8217;t dramatic. </p>  <p><span>But the more items there are, the bigger the difference.</span></p>  <p> </p>  <p><span>repeatedly dividing a range (from the first column) in half until it&#8217;s too small to divide further.</span></p>  <p><span>Given the range, we want to know how many comparisons it will take to complete a search. That is, given r, we want an equation that gives us s. (Logorithm)</span></p>  <p style="text-align:center" align="center"><strong><span style="color:red;">s = log<sub>2</sub>(r)</span></strong></p>  <p><span>you can convert easily to base 2 (<strong><span style="color:red">log<sub>2</sub></span></strong>)to base 10 by multiplying by <span style="color:red">3.322</span></span></p>  <p>For example, <span style="color:red">log<sub>10</sub>(100) = 2, so <strong><span style="color:red">log<sub>2</sub></span></strong> (100) = 2 times <span style="color:red">3.322</span>, or 6.644. Rounded up to 7</span></p>  <p><strong>&nbsp;</strong></p>  <p><strong><span style="font-size:14.0pt;line-height:115%;">Big O Notation</span></strong></p>  <p style="text-indent:.5in"><span style="font-size:12.0pt;line-height:115%;">how <strong>efficient</strong> a computer algorithm</span></p>  <p style="margin-left:.75in; text-indent:-.25in;"><strong><span style="font-size:12.0pt;line-height:115%;"><span>1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></strong><strong><span style="font-size:12.0pt;line-height:115%;">Inserting into an Unordered Array: Constant</span></strong></p>  <p style="margin-left:.75in;"><span>depend on how many items are in the array. The new item is always placed in the next available position, at a[nElems], and nElemsis then incremented.</span></p>  <p style="margin-left:.5in"><span>We can say the time, T, to insert an item into an unsorted array is a constant K<span style="color:red">: </span></span><strong><span style="font-size:12.0pt;line-height:115%;color:red;">T = K</span></strong></p>  <p style="margin-left:.75in; text-indent:-.25in;"><strong><span style="font-size:12.0pt;line-height:115%;"><span>2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></strong><strong><span style="font-size:12.0pt;line-height:115%;">Linear Searching: Proportional to N</span></strong></p>  <p style="margin-left:.75in;text-indent:-.25in;"><strong><span style="font-size:12.0pt;line-height:115%;"><span>3.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></strong><strong><span style="font-size:12.0pt;line-height:115%;">a linear search of items in an array, the number of comparisons that</span></strong></p>  <p style="margin-left:.75in;"><span>must be made to find a specified item is, on the <strong>average,</strong> half of the total number of</span></p>  <p style="margin-left:.75in;">items.</p>  <p style="margin-left:.75in;text-align:center" align="center"><strong><span style="color:red;">T = K * N / 2</span></strong></p>  <p style="margin-left:.75in; text-indent:-.25in;"><strong><span style="font-size:12.0pt;line-height:115%;"><span>4.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></strong><strong><span style="font-size:12.0pt;line-height:115%;">Binary Searching: Proportional to log(N)</span></strong></p>  <p style="margin-left:.5in;text-align:center" align="center"><strong><span style="font-size:12.0pt;line-height:115%;color:red;">T = K * log2(N)</span></strong></p>  <p style="margin-left:.5in"><span style="font-size:16.0pt;line-height:115%;">Eliminating the Constant K</span></p>  <p style="margin-left:.5in"><span>Big O notation uses the uppercase letter O, which you can think of as meaning &nbsp;&#8220;order</span></p>  <p style="margin-left:.5in">of.&#8221;</p>  <p style="margin-left:.5in"><span>In Big O notation, we would say that a linear search takes O(N) time, and a binary</span></p>  <p style="margin-left:.5in"><span>search takes O(log N) time.</span></p>  <p style="margin-left:.5in"> </p>  <p><span>   &nbsp;</span></p>  <p> </p>  <p>Figure 3.5 graphs some Big O relationships between time and number of items. Based on</p>  <p>this graph, we might rate the various Big O values (very subjectively) like this: O(1) is</p>  <p>excellent, O(log N) is good, O(N) is fair, and O(N2) is poor. O(N2) occurs in certain sort-ing algorithms that we&#8217;ll look at in Hours 4 and 5.</p>  <p> </p>  <h2>&nbsp;</h2>  <h2>Hour4: Bubble Sort (sorting)</h2>  <p>To Do: Bubble-Sort the Baseball Players</p>  <p> </p>  <p>1. Compare two players.</p>  <p>2. If the one on the left is taller, swap them.</p>  <p>3. Move one position right.</p>  <p>You continue down the line this way until you reach the right end. You have by no means finished sorting the kids, but you do know that the tallest kid is on the right.</p>  <p> </p>  <p>&nbsp;</p>  <h3>Efficiency of the Bubble Sort(how&nbsp;fast)</h3>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;&nbsp;i&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;nElems&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;&nbsp;</span><span style="color: #000000; ">++</span><span style="color: #000000; ">i)<br /></span><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;times&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;j&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;i&nbsp;</span><span style="color: #000000; ">+</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;&nbsp;j&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;nElems;&nbsp;</span><span style="color: #000000; ">++</span><span style="color: #000000; ">j)<br /></span><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">&nbsp;8</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;times&nbsp;</span><span style="color: #000000; ">+=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">10</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">11</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(v[i]&nbsp;</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;v[j])<br /></span><span style="color: #008080; ">12</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">13</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swap(i,&nbsp;j);<br /></span><span style="color: #008080; ">14</span>&nbsp;<span style="color: #000000; "><br /></span><span style="color: #008080; ">15</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /></span></div><br /><p><span style="font-size:10.0pt;line-height:115%;font-family: &quot;YaHei Consolas Hybrid&quot;;YaHei Consolas Hybrid&quot;;color:black">For 10 items this is</span></p>  <p><span style="font-size:10.0pt;line-height:115%;font-family: &quot;YaHei Consolas Hybrid&quot;;YaHei Consolas Hybrid&quot;;color:black">9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45&nbsp;&nbsp; </span></p>  <p>&nbsp;</p>  <p>In general, N items:</p>  <p><strong><span style="font-size:14.0pt;line-height:115%">(N-1) + (N-2) + (N-3) + ... + 1 = N*(N-1)/2</span></strong></p>  <p>N*(N-1)/2 &nbsp;is 45 when N is 10.</p>  <p>Thus the algorithm makes about <strong>N<sup>2</sup>/2</strong> comparisons</p>  <p>Because constants don&#8217;t count in Big O notation in Big O notation, we can ignore the divisors 2 and 4 and say that the bubble sort runs in <strong><span style="color:red">O(N2)</span></strong> time. And this is slow</p>  <p>Whenever you see nested loops such as those in the bubble sort, you can suspect that an algorithm runs in O(N<sup>2</sup>) time. The outer loop executes N times, and the inner loop exe-cutes N (or perhaps N divided by some constant) times for each cycle of the outer loop.</p>  <p>This means you&#8217;re doing something approximately N*N or N<sup>2</sup> times.</p>  <h2>&nbsp;</h2>  <h2>HOUR5 The Insertion Sort</h2>  <p>&nbsp;</p>  <p>&nbsp;&#8220;The Bubble Sort,&#8221; is the easiest sort to understand. However, it&#8217;s also the least sophisticated. We can improve it at the cost of some additional complexity. It still executes in O(N<sup>2</sup>) time, but it&#8217;s about twice as fast as the bubble sort.</p>  <p>It&#8217;s often used as the final stage of more sophisticated sorts, such as <strong>quicksort</strong>.</p>  <p><strong><span style="font-size:14.0pt;line-height:115%">Insertion Sort on the Baseball Players</span></strong></p>  <p>&nbsp;&nbsp;&nbsp; It&#8217;s easier to think about the insertion sort if we begin in the middle of the process, when the team is partly sorted</p>  <p><strong><span style="font-size:12.0pt;line-height:115%">Partial Sorting(Inserting the Marked Player in the Appropriate Location)</span></strong></p>  <p>The player where the marker is, whom we&#8217;ll call the &#8220;marked&#8221; player, &nbsp;and all the players on her right, &nbsp;&nbsp;is as yet unsorted.</p>  <p style="text-indent:-.25in;"><span><span>1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>insert the marked player in the appropriate place in the (partially) sorted group.</p>  <p>We take the marked player out of line, and shift the sorted players to make room</p>  <p><strong>When shifting process stop? </strong></p>  <p>when you&#8217;ve shifted the <strong>last</strong> player who is taller than the marked player.</p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;insertionSort()&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">out</span><span style="color: #000000; ">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">in</span><span style="color: #000000; ">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">out</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;&nbsp;</span><span style="color: #0000FF; ">out</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">&nbsp;nElems;&nbsp;</span><span style="color: #000000; ">++</span><span style="color: #0000FF; ">out</span><span style="color: #000000; ">)&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;temp&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;v[</span><span style="color: #0000FF; ">out</span><span style="color: #000000; ">];&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;remove&nbsp;marked&nbsp;item&nbsp;,&nbsp;make&nbsp;room&nbsp;for&nbsp;replace</span><span style="color: #008000; "><br /></span><span style="color: #000000; "><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">in</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">out</span><span style="color: #000000; ">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">in</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">&amp;&amp;</span><span style="color: #000000; ">&nbsp;v[</span><span style="color: #0000FF; ">in</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">]&nbsp;</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;temp)&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v[</span><span style="color: #0000FF; ">in</span><span style="color: #000000; ">]&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;v[</span><span style="color: #0000FF; ">in</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">--</span><span style="color: #0000FF; ">in</span><span style="color: #000000; ">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v[</span><span style="color: #0000FF; ">in</span><span style="color: #000000; ">]&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;temp;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />}</span></div>&nbsp; <h3><span style="font-size:14.0pt;line-height:115%">Efficiency of the Insertion Sort</span></h3>  <p>How many comparisons and copies does this algorithm require? </p>  <p>On the first pass, it compares a maximum of one item. On the second pass, it&#8217;s a maximum of two items, and so on, up to a maximum of N&#8211;1 comparisons on the last pass.</p>  <p>1 + 2 + 3 + ... + N-1 = <strong>N*(N-1)/2</strong></p>  <p>However, because on each pass an average of only half of the maximum number of items</p>  <p>are actually compared before the insertion point is found, we can divide by 2, which gives the following equation:</p>  <p><strong>N*(N-1)/4</strong></p>  <p>For data that is already sorted or almost sorted, the insertion sort does much better. </p>  <p>When data is in order, the condition in the while loop is never true, so it becomes a simple statement in the outer loop, which executes <strong>N&#8211;1</strong> times. In this case the algorithm runs in O(N) time. If the data is almost sorted, insertion sort runs in almost O(N) time, which makes it a simple and efficient way to order a file that is only slightly out of order.</p>  <p>However, for <strong>data arranged in inverse sorted order</strong>, every possible comparison and shift is</p>  <p>carried out, so the insertion sort runs no faster than the bubble sort.</p>  <p style="text-indent:.5in"><strong>Q</strong> If both the bubble sort and the insertion sort run in O(N<sup>2</sup>) time, why not use</p>  <p>the bubble sort, which is simpler to program?</p>  <p style="text-indent:.5in"><strong>A</strong> Besides being somewhat faster for random data, the insertion sort is much faster</p>  <p>for data that is only slightly out of order.</p><img src ="http://www.cppblog.com/ylxin1993/aggbug/196577.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ylxin1993/" target="_blank">Johnson Yuan</a> 2012-12-24 20:18 <a href="http://www.cppblog.com/ylxin1993/archive/2012/12/24/196577.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>