<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>Pherg.net</title>
	<link>http://www.pherg.net</link>
	<description>using the right tool for the right job</description>
	<pubDate>Tue, 03 Jun 2008 17:52:57 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
	<language>en</language>
			<item>
		<title>C# Introsort</title>
		<link>http://www.pherg.net/2008/02/27/c-introsort/</link>
		<comments>http://www.pherg.net/2008/02/27/c-introsort/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 17:22:00 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[heapsort]]></category>

		<category><![CDATA[introsort]]></category>

		<category><![CDATA[quicksort]]></category>

		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://www.pherg.net/2008/02/27/c-introsort/</guid>
		<description><![CDATA[Here is a C# implementation of the Introsort algorithm. Notice that the Introsort method is nearly identical to the Quicksort method. The only difference is the addition of checking for the depth limit (number of recursions &#8212; lines 51-59); when the depth limit has been reached, the Heapsort method is called to complete the sort of the [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a C# implementation of the Introsort algorithm. Notice that the Introsort method is nearly identical to the Quicksort method. The only difference is the addition of checking for the depth limit (number of recursions &#8212; lines 51-59); when the depth limit has been reached, the Heapsort method is called to complete the sort of the remaining items of the present partition. See <a target="_blank" href="http://www.cs.rpi.edu/~musser/gp/introsort.ps">David Musser&#8217;s paper</a> for an in-depth analysis of the Introsort algorithm, as well as a discussion about choosing the correct recursion depth.</p>
<p>This code is used to sort collections of objects of any class that implement IComparable. It is optimized for readability, not performance (if I were worried about performance I would have used the &#8220;unsafe&#8221; keyword and sorted everything using pointers), and includes functions for Quicksort, Heapsort, and Introsort, as well as some helper methods (Swap, SiftDown).</p>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   1:  </span><span style="color: #cc6633">#region Copyright (c) 2008 Michael Ferguson [www.pherg.net]</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   2:  </span><span style="color: #008000">/*****************************************************************************</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   3:  </span><span style="color: #008000"> * File:   Sorter.cs</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   4:  </span><span style="color: #008000"> * Author: Michael Ferguson (http://www.pherg.net)</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   5:  </span><span style="color: #008000"> * Date:   19 February 2008</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   6:  </span><span style="color: #008000"> * </span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   7:  </span><span style="color: #008000"> * This program is free software: you can redistribute it and/or modify it</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   8:  </span><span style="color: #008000"> * under the terms of the GNU General Public License as published by the Free</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   9:  </span><span style="color: #008000"> * Software Foundation, either version 3 of the License, or (at your option)</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  10:  </span><span style="color: #008000"> * any later version.</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  11:  </span><span style="color: #008000"> *</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  12:  </span><span style="color: #008000"> * This program is distributed in the hope that it will be useful, but WITHOUT</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  13:  </span><span style="color: #008000"> * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  14:  </span><span style="color: #008000"> * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  15:  </span><span style="color: #008000"> * more details (http://www.gnu.org/licenses/gpl.txt).</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  16:  </span><span style="color: #008000"> ****************************************************************************/</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  17:  </span><span style="color: #cc6633">#endregion</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  18:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  19:  </span><span style="color: #0000ff">using</span> System;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  20:  </span><span style="color: #0000ff">using</span> System.Collections.Generic;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  21:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  22:  </span><span style="color: #0000ff">namespace</span> Pherg</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  23:  </span>{</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  24:  </span>  <span style="color: #0000ff">class</span> Sorter&lt;T&gt; <span style="color: #0000ff">where</span> T:IComparable</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  25:  </span>  {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  26:  </span>    <span style="color: #0000ff">public</span> Sorter ()</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  27:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  28:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  29:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  30:  </span>    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Introsort (T[] array, <span style="color: #0000ff">int</span> left, <span style="color: #0000ff">int</span> right, <span style="color: #0000ff">int</span> depthLimit)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  31:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  32:  </span>      <span style="color: #0000ff">if</span> (left &gt;= right)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  33:  </span>      {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  34:  </span>        <span style="color: #008000">// done sorting</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  35:  </span>        <span style="color: #0000ff">return</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  36:  </span>      }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  37:  </span>      <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (left == right - <span style="color: red">1</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  38:  </span>      {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  39:  </span>        <span style="color: #008000">// two element list</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  40:  </span>        <span style="color: #0000ff">if</span> (array[left].CompareTo(array[right]) &gt; <span style="color: red">0</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  41:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  42:  </span>          <span style="color: #008000">// left is greater than the right &#8212; swap them</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  43:  </span>          Swap(array, left, right);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  44:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  45:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  46:  </span>        <span style="color: #008000">// done sorting</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  47:  </span>        <span style="color: #0000ff">return</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  48:  </span>      }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  49:  </span>      <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (depthLimit == <span style="color: red">0</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  50:  </span>      {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  51:  </span>        <span style="color: #008000">// depth limit has reached maximum</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  52:  </span>        Heapsort(array, left, right);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  53:  </span>        <span style="color: #0000ff">return</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  54:  </span>      }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  55:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  56:  </span>      <span style="color: #008000">// decrement depth limit</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  57:  </span>      depthLimit -= 1;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  58:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  59:  </span>      <span style="color: #008000">// since this method is recursive avoid placing these variables on the</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  60:  </span>      <span style="color: #008000">//   stack until the last moment</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  61:  </span>      <span style="color: #0000ff">int</span> i = left;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  62:  </span>      <span style="color: #0000ff">int</span> k = right;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  63:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  64:  </span>      <span style="color: #008000">// choose a pivot and move it to the end of the list</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  65:  </span>      <span style="color: #0000ff">int</span> pivotIndex = (left + right) &gt;&gt; <span style="color: red">1</span>; <span style="color: #008000">// (left + right) / 2</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  66:  </span>      T pivot = array[pivotIndex];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  67:  </span>      array[pivotIndex] = array[right];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  68:  </span>      array[right] = pivot;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  69:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  70:  </span>      <span style="color: #0000ff">while</span> (i &lt; k)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  71:  </span>      {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  72:  </span>        <span style="color: #008000">// move the i index to the k until something is found that is</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  73:  </span>        <span style="color: #008000">//  greater than the pivot</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  74:  </span>        <span style="color: #0000ff">while</span> ((array[i].CompareTo(pivot) &lt;= <span style="color: red">0</span>) &amp;&amp; (i &lt; k))</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  75:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  76:  </span>          i++;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  77:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  78:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  79:  </span>        <span style="color: #008000">// move the k index to the i until something is found that is</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  80:  </span>        <span style="color: #008000">//  less than the pivot</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  81:  </span>        <span style="color: #0000ff">while</span> ((pivot.CompareTo(array[k]) &lt;= <span style="color: red">0</span>) &amp;&amp; (i &lt; k))</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  82:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  83:  </span>          k -= 1;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  84:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  85:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  86:  </span>        <span style="color: #0000ff">if</span> (i &lt; k)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  87:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  88:  </span>          <span style="color: #008000">// swap i and k</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  89:  </span>          Swap(array, i, k);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  90:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  91:  </span>      }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  92:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  93:  </span>      <span style="color: #008000">// put the pivot back</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  94:  </span>      array[right] = array[k];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  95:  </span>      array[k] = pivot;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  96:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  97:  </span>      <span style="color: #008000">// sort the left side</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  98:  </span>      Introsort(array, left, i - <span style="color: red">1</span>, depthLimit);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  99:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 100:  </span>      <span style="color: #008000">// sort the right side</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 101:  </span>      Introsort(array, k + <span style="color: red">1</span>, right, depthLimit);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 102:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 103:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 104:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 105:  </span>    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Quicksort (T[] array, <span style="color: #0000ff">int</span> left, <span style="color: #0000ff">int</span> right)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 106:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 107:  </span>      <span style="color: #0000ff">if</span> (left &gt;= right)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 108:  </span>      {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 109:  </span>        <span style="color: #008000">// done sorting</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 110:  </span>        <span style="color: #0000ff">return</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 111:  </span>      }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 112:  </span>      <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (left == right - <span style="color: red">1</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 113:  </span>      {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 114:  </span>        <span style="color: #008000">// two element list</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 115:  </span>        <span style="color: #0000ff">if</span> (array[left].CompareTo(array[right]) &gt; <span style="color: red">0</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 116:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 117:  </span>          <span style="color: #008000">// left is greater than the right &#8212; swap them</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 118:  </span>          Swap(array, left, right);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 119:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 120:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 121:  </span>        <span style="color: #008000">// done sorting</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 122:  </span>        <span style="color: #0000ff">return</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 123:  </span>      }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 124:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 125:  </span>      <span style="color: #008000">// since this method is recursive avoid placing these variables on the</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 126:  </span>      <span style="color: #008000">//   stack until the last moment</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 127:  </span>      <span style="color: #0000ff">int</span> i = left;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 128:  </span>      <span style="color: #0000ff">int</span> k = right;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 129:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 130:  </span>      <span style="color: #008000">// choose a pivot and move it to the end of the list</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 131:  </span>      <span style="color: #0000ff">int</span> pivotIndex = (left + right) &gt;&gt; <span style="color: red">1</span>; <span style="color: #008000">// (left + right) / 2</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 132:  </span>      T pivot = array[pivotIndex];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 133:  </span>      array[pivotIndex] = array[right];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 134:  </span>      array[right] = pivot;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 135:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 136:  </span>      <span style="color: #0000ff">while</span> (i &lt; k)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 137:  </span>      {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 138:  </span>        <span style="color: #008000">// move the i index to the k until something is found that is</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 139:  </span>        <span style="color: #008000">//  greater than the pivot</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 140:  </span>        <span style="color: #0000ff">while</span> ((array[i].CompareTo(pivot) &lt;= <span style="color: red">0</span>) &amp;&amp; (i &lt; k))</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 141:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 142:  </span>          i++;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 143:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 144:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 145:  </span>        <span style="color: #008000">// move the k index to the i until something is found that is</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 146:  </span>        <span style="color: #008000">//  less than the pivot</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 147:  </span>        <span style="color: #0000ff">while</span> ((pivot.CompareTo(array[k]) &lt;= <span style="color: red">0</span>) &amp;&amp; (i &lt; k))</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 148:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 149:  </span>          k -= 1;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 150:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 151:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 152:  </span>        <span style="color: #0000ff">if</span> (i &lt; k)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 153:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 154:  </span>          <span style="color: #008000">// swap i and k</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 155:  </span>          Swap(array, i, k);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 156:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 157:  </span>      }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 158:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 159:  </span>      <span style="color: #008000">// put the pivot back</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 160:  </span>      array[right] = array[k];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 161:  </span>      array[k] = pivot;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 162:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 163:  </span>      <span style="color: #008000">// sort the left side</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 164:  </span>      Quicksort(array, left, i - <span style="color: red">1</span>);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 165:  </span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 166:  </span>      <span style="color: #008000">// sort the right side</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 167:  </span>      Quicksort(array, k + <span style="color: red">1</span>, right);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 168:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 169:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 170:  </span>    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Heapsort (T[] array, <span style="color: #0000ff">int</span> start, <span style="color: #0000ff">int</span> end)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 171:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 172:  </span>      <span style="color: #008000">// to begin, place the array in max-heap order</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 173:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 174:  </span>      <span style="color: #008000">// get the index in array of the last parent node</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 175:  </span>      <span style="color: #0000ff">int</span> left = start + ((end - start) &gt;&gt; <span style="color: red">1</span>);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 176:  </span>      <span style="color: #0000ff">int</span> right = end;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 177:  </span>      <span style="color: #0000ff">int</span> offset = start;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 178:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 179:  </span>      <span style="color: #008000">//while (left &gt;= 0)</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 180:  </span>      <span style="color: #0000ff">while</span> (left &gt;= start)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 181:  </span>      {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 182:  </span>        <span style="color: #008000">// sift down the node at index left to the proper place so that all</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 183:  </span>        <span style="color: #008000">//   nodes below the left index are in heap order</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 184:  </span>        <span style="color: #008000">//SiftDown(array, left, array.Length - 1);</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 185:  </span>        SiftDown(array, offset, left, end);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 186:  </span>        left -= <span style="color: red">1</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 187:  </span>      }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 188:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 189:  </span>      <span style="color: #008000">// all nodes should now be in max-heap order</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 190:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 191:  </span>      <span style="color: #0000ff">while</span> (right &gt; start)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 192:  </span>      {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 193:  </span>        <span style="color: #008000">// swap the maximum value of the heap with the last element of the</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 194:  </span>        <span style="color: #008000">//   heap</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 195:  </span>        Swap(array, right, start);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 196:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 197:  </span>        <span style="color: #008000">// decrease the size of the heap by one so that the previous max</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 198:  </span>        <span style="color: #008000">//   value will stay in its proper placement</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 199:  </span>        right -= <span style="color: red">1</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 200:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 201:  </span>        <span style="color: #008000">// put the heap back in max-heap order</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 202:  </span>        SiftDown(array, offset, start, right);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 203:  </span>      }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 204:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 205:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 206:  </span>    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> SiftDown (T[] array, <span style="color: #0000ff">int</span> offset, <span style="color: #0000ff">int</span> start, <span style="color: #0000ff">int</span> end)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 207:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 208:  </span>      <span style="color: #0000ff">int</span> root = start;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 209:  </span>      <span style="color: #0000ff">int</span> child;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 210:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 211:  </span>      <span style="color: #008000">// keep going while the root has at least one child</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 212:  </span>      <span style="color: #0000ff">while</span> (((root - offset) * <span style="color: red">2</span> + <span style="color: red">1</span>) &lt;= (end - offset))</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 213:  </span>      {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 214:  </span>        <span style="color: #008000">// get the left child</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 215:  </span>        child = ((root - offset) * <span style="color: red">2</span> + <span style="color: red">1</span>) + offset;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 216:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 217:  </span>        <span style="color: #008000">// if the child has a sibling and the child&#8217;s value is less than its</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 218:  </span>        <span style="color: #008000">//   siblings</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 219:  </span>        <span style="color: #0000ff">if</span> (child &lt; end &amp;&amp; array[child].CompareTo(array[child + <span style="color: red">1</span>]) &lt; <span style="color: red">0</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 220:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 221:  </span>          <span style="color: #008000">// point to the right child instead</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 222:  </span>          child += <span style="color: red">1</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 223:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 224:  </span>        <span style="color: #0000ff">if</span> (array[root].CompareTo(array[child]) &lt; <span style="color: red">0</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 225:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 226:  </span>          <span style="color: #008000">// out of max-heap order</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 227:  </span>          Swap(array, root, child);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 228:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 229:  </span>          <span style="color: #008000">// repeat to continue sifting down the child</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 230:  </span>          root = child;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 231:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 232:  </span>        <span style="color: #0000ff">else</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 233:  </span>        {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 234:  </span>          <span style="color: #008000">// all sifted</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 235:  </span>          <span style="color: #0000ff">break</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 236:  </span>        }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 237:  </span>      }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 238:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 239:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 240:  </span>    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> Swap (T[] array, <span style="color: #0000ff">int</span> i, <span style="color: #0000ff">int</span> k)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 241:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 242:  </span>      T swap = array[i];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 243:  </span>      array[i] = array[k];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 244:  </span>      array[k] = swap;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 245:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060"> 246:  </span>  }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060"> 247:  </span>}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pherg.net/2008/02/27/c-introsort/feed/</wfw:commentRss>
		</item>
		<item>
		<title>C# Heapsort</title>
		<link>http://www.pherg.net/2008/02/15/c-heapsort/</link>
		<comments>http://www.pherg.net/2008/02/15/c-heapsort/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 15:01:20 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[.net]]></category>

		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://www.pherg.net/2008/02/15/c-heapsort/</guid>
		<description><![CDATA[This is a simple implementation of the Heapsort algorithm in C#. It is a max-heap implementation, meaning that it builds the heap from the bottom up by sifting down the maximum element; this is opposed to the min-heap implementation that builds the heap top-down by sifting upward. Also, this method is a member of the Sorting [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple implementation of the Heapsort algorithm in C#. It is a max-heap implementation, meaning that it builds the heap from the bottom up by sifting down the maximum element; this is opposed to the min-heap implementation that builds the heap top-down by sifting upward. Also, this method is a member of the <a href="http://www.pherg.net/2008/02/27/c-introsort/">Sorting class that I will present later</a>, and the generic class T is defined in this context as any class that implements IComparable.</p>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   1:  </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Heapsort (T[] array, <span style="color: #0000ff">int</span> start, <span style="color: #0000ff">int</span> end)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   2:  </span>{</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   3:  </span>  <span style="color: #008000">// to begin, place the array in max-heap order</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   4:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   5:  </span>  <span style="color: #008000">// get the index in array of the last parent node</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   6:  </span>  <span style="color: #0000ff">int</span> left = start + ((end - start) &gt;&gt; <span style="color: red">1</span>);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   7:  </span>  <span style="color: #0000ff">int</span> right = end;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   8:  </span>  <span style="color: #0000ff">int</span> offset = start;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   9:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  10:  </span>  <span style="color: #008000">//while (left &gt;= 0)</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  11:  </span>  <span style="color: #0000ff">while</span> (left &gt;= start)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  12:  </span>  {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  13:  </span>    <span style="color: #008000">// sift down the node at index left to the proper place so that all</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  14:  </span>    <span style="color: #008000">//   nodes below the left index are in heap order</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  15:  </span>    <span style="color: #008000">//SiftDown(array, left, array.Length - 1);</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  16:  </span>    SiftDown(array, offset, left, end);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  17:  </span>    left -= <span style="color: red">1</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  18:  </span>  }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  19:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  20:  </span>  <span style="color: #008000">// all nodes should now be in max-heap order</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  21:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  22:  </span>  <span style="color: #0000ff">while</span> (right &gt; start)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  23:  </span>  {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  24:  </span>    <span style="color: #008000">// swap the maximum value of the heap with the last element of the</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  25:  </span>    <span style="color: #008000">//   heap</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  26:  </span>    Swap(array, right, start);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  27:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  28:  </span>    <span style="color: #008000">// decrease the size of the heap by one so that the previous max</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  29:  </span>    <span style="color: #008000">//   value will stay in its proper placement</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  30:  </span>    right -= <span style="color: red">1</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  31:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  32:  </span>    <span style="color: #008000">// put the heap back in max-heap order</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  33:  </span>    SiftDown(array, offset, start, right);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  34:  </span>  }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  35:  </span>}</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  36:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  37:  </span><span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> SiftDown (T[] array, <span style="color: #0000ff">int</span> offset, <span style="color: #0000ff">int</span> start, <span style="color: #0000ff">int</span> end)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  38:  </span>{</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  39:  </span>  <span style="color: #0000ff">int</span> root = start;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  40:  </span>  <span style="color: #0000ff">int</span> child;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  41:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  42:  </span>  <span style="color: #008000">// keep going while the root has at least one child</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  43:  </span>  <span style="color: #0000ff">while</span> (((root - offset) * <span style="color: red">2</span> + <span style="color: red">1</span>) &lt;= (end - offset))</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  44:  </span>  {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  45:  </span>    <span style="color: #008000">// get the left child</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  46:  </span>    child = ((root - offset) * <span style="color: red">2</span> + <span style="color: red">1</span>) + offset;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  47:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  48:  </span>    <span style="color: #008000">// if the child has a sibling and the child&#8217;s value is less than its</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  49:  </span>    <span style="color: #008000">//   siblings</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  50:  </span>    <span style="color: #0000ff">if</span> (child &lt; end &amp;&amp; array[child].CompareTo(array[child + <span style="color: red">1</span>]) &lt; <span style="color: red">0</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  51:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  52:  </span>      <span style="color: #008000">// point to the right child instead</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  53:  </span>      child += <span style="color: red">1</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  54:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  55:  </span>    <span style="color: #0000ff">if</span> (array[root].CompareTo(array[child]) &lt; <span style="color: red">0</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  56:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  57:  </span>      <span style="color: #008000">// out of max-heap order</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  58:  </span>      Swap(array, root, child);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  59:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  60:  </span>      <span style="color: #008000">// repeat to continue sifting down the child</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  61:  </span>      root = child;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  62:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  63:  </span>    <span style="color: #0000ff">else</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  64:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  65:  </span>      <span style="color: #008000">// all sifted</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  66:  </span>      <span style="color: #0000ff">break</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  67:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  68:  </span>  }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  69:  </span>}</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  70:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  71:  </span><span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> Swap (T[] array, <span style="color: #0000ff">int</span> i, <span style="color: #0000ff">int</span> k)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  72:  </span>{</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  73:  </span>  T swap = array[i];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  74:  </span>  array[i] = array[k];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  75:  </span>  array[k] = swap;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  76:  </span>}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pherg.net/2008/02/15/c-heapsort/feed/</wfw:commentRss>
		</item>
		<item>
		<title>.NET Platform Invoke (P/Invoke) Sample</title>
		<link>http://www.pherg.net/2008/02/12/net-platform-invoke-pinvoke-sample/</link>
		<comments>http://www.pherg.net/2008/02/12/net-platform-invoke-pinvoke-sample/#comments</comments>
		<pubDate>Tue, 12 Feb 2008 17:03:58 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[DLL]]></category>

		<category><![CDATA[P/Invoke]]></category>

		<guid isPermaLink="false">http://www.pherg.net/2008/02/12/net-platform-invoke-pinvoke-sample/</guid>
		<description><![CDATA[Recently someone asked me to help them call an unmanaged C++ DLL from a C# application, whereupon I promptly pointed them to the MSDN documentation on P/Invoke. Unfortunately (as I later learned) the documentation is very descriptive if you&#8217;re trying to use a Windows DLL, but it doesn&#8217;t help you understand how to create the [...]]]></description>
			<content:encoded><![CDATA[<p>Recently someone asked me to help them call an unmanaged C++ DLL from a C# application, whereupon I promptly pointed them to the MSDN documentation on P/Invoke. Unfortunately (as I later learned) the documentation is very descriptive if you&#8217;re trying to use a Windows DLL, but it doesn&#8217;t help you understand how to create the unmanaged C++ DLL in the first place. Here&#8217;s how you do it:</p>
<ol>
<li>In Visual C++ (I use 2008) create a new Win32 Project, and then choose &#8220;DLL&#8221; in the &#8220;Application Settings&#8221; page of the Create Project wizard that follows.</li>
<li>If the project name I had chosen was &#8220;TestDll,&#8221; I would have a file named TestDll.cpp in my project. This is where we&#8217;ll put our functions to be called by C#. Open this file for editing.</li>
<li>Add a function that performs the action you want. I will create a function (for demonstration purposes only) called AddNumbers that, obviously, adds two numbers and returns the result (the __declspec(dllexport) tells the C++ compiler to extern this function):
<pre><font size="2" color="#0000ff">extern</font><font size="2"> </font><font size="2" color="#a31515">&#8220;C&#8221;</font><font size="2"> </font><font size="2" color="#0000ff">__declspec</font><font size="2">(</font><font size="2" color="#0000ff">dllexport</font><font size="2">)</font><font size="2" color="#0000ff">int</font><font size="2"> AddNumbers(</font><font size="2" color="#0000ff">int</font><font size="2"> a, </font><font size="2" color="#0000ff">int</font><font size="2"> b)</font><font size="2">
{
<font size="2" color="#0000ff">  return</font><font size="2"> a + b;</font>
<font size="2">}
</font>
</font></pre>
</li>
<li>Compile this project.</li>
<li>Create a C# application and extern the C++ DLL function as follows. This tells the C# compiler that this function will be available at runtime:
<pre><font size="2">[</font><font size="2" color="#2b91af">DllImport</font><font size="2">(&#8221;</font><font size="2" color="#a31515">TestDll.dll&#8221;</font><font size="2">)]</font>
<font size="2"><font size="2" color="#0000ff">static</font><font size="2"> </font><font size="2" color="#0000ff">extern</font><font size="2"> </font><font size="2" color="#0000ff">int</font><font size="2"> AddNumbers(</font><font size="2" color="#0000ff">int</font><font size="2"> a, </font><font size="2" color="#0000ff">int</font><font size="2"> b);</font>    

</font></pre>
</li>
<li>Then you can just call AddNumbers from C# just as if it was another function in your C# project.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.pherg.net/2008/02/12/net-platform-invoke-pinvoke-sample/feed/</wfw:commentRss>
		</item>
		<item>
		<title>C# Quicksort</title>
		<link>http://www.pherg.net/2007/12/10/c-quicksort/</link>
		<comments>http://www.pherg.net/2007/12/10/c-quicksort/#comments</comments>
		<pubDate>Tue, 11 Dec 2007 03:43:58 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[.net]]></category>

		<category><![CDATA[quicksort]]></category>

		<guid isPermaLink="false">http://www.pherg.net/2007/12/10/c-quicksort/</guid>
		<description><![CDATA[Here is a simple implementation of the Quicksort sorting algorithm in C#. This class uses generics so any type that implements IComparable can be sorted. This code is neither intended to be the most efficient Quicksort implementation, nor does it implement Median-of-Three and Insertion sort enhancements; it does, however, provide an easy-to-follow simple implementation of [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a simple implementation of the Quicksort sorting algorithm in C#. This class uses generics so any type that implements <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.icomparable.aspx">IComparable</a> can be sorted. This code is neither intended to be the most efficient Quicksort implementation, nor does it implement Median-of-Three and Insertion sort enhancements; it does, however, provide an easy-to-follow simple implementation of the algorithm. This will provide a solid foundation for the Introsort algorithm that will be presented later. Also, this method is a member of the Sorting class that I will present later, and the generic class T is defined in this context as any class that implements IComparable.</p>
<p style="font-size: small; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff">
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   1:  </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Quicksort (T[] array, <span style="color: #0000ff">int</span> left, <span style="color: #0000ff">int</span> right)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   2:  </span>{</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   3:  </span>  <span style="color: #0000ff">if</span> (left &gt;= right)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   4:  </span>  {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   5:  </span>    <span style="color: #008000">// done sorting</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   6:  </span>    <span style="color: #0000ff">return</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   7:  </span>  }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">   8:  </span>  <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (left == right - <span style="color: red">1</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">   9:  </span>  {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  10:  </span>    <span style="color: #008000">// two element list</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  11:  </span>    <span style="color: #0000ff">if</span> (array[left].CompareTo(array[right]) &gt; <span style="color: red">0</span>)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  12:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  13:  </span>      <span style="color: #008000">// left is greater than the right &#8212; swap them</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  14:  </span>      Swap(array, left, right);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  15:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  16:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  17:  </span>    <span style="color: #008000">// done sorting</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  18:  </span>    <span style="color: #0000ff">return</span>;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  19:  </span>  }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  20:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  21:  </span>  <span style="color: #008000">// since this method is recursive avoid placing these variables on the</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  22:  </span>  <span style="color: #008000">//   stack until the last moment</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  23:  </span>  <span style="color: #0000ff">int</span> i = left;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  24:  </span>  <span style="color: #0000ff">int</span> k = right;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  25:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  26:  </span>  <span style="color: #008000">// choose a pivot and move it to the end of the list</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  27:  </span>  <span style="color: #0000ff">int</span> pivotIndex = (left + right) &gt;&gt; <span style="color: red">1</span>; <span style="color: #008000">// (left + right) / 2</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  28:  </span>  T pivot = array[pivotIndex];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  29:  </span>  array[pivotIndex] = array[right];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  30:  </span>  array[right] = pivot;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  31:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  32:  </span>  <span style="color: #0000ff">while</span> (i &lt; k)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  33:  </span>  {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  34:  </span>    <span style="color: #008000">// move the i index to the k until something is found that is</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  35:  </span>    <span style="color: #008000">//  greater than the pivot</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  36:  </span>    <span style="color: #0000ff">while</span> ((array[i].CompareTo(pivot) &lt;= <span style="color: red">0</span>) &amp;&amp; (i &lt; k))</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  37:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  38:  </span>      i++;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  39:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  40:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  41:  </span>    <span style="color: #008000">// move the k index to the i until something is found that is</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  42:  </span>    <span style="color: #008000">//  less than the pivot</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  43:  </span>    <span style="color: #0000ff">while</span> ((pivot.CompareTo(array[k]) &lt;= <span style="color: red">0</span>) &amp;&amp; (i &lt; k))</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  44:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  45:  </span>      k&#8211;;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  46:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  47:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  48:  </span>    <span style="color: #0000ff">if</span> (i &lt; k)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  49:  </span>    {</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  50:  </span>      <span style="color: #008000">// swap i and k</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  51:  </span>      Swap(array, i, k);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  52:  </span>    }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  53:  </span>  }</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  54:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  55:  </span>  <span style="color: #008000">// put the pivot back</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  56:  </span>  array[right] = array[k];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  57:  </span>  array[k] = pivot;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  58:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  59:  </span>  <span style="color: #008000">// sort the left side</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  60:  </span>  Quicksort(array, left, i - <span style="color: red">1</span>);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  61:  </span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  62:  </span>  <span style="color: #008000">// sort the right side</span></pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  63:  </span>  Quicksort(array, k + <span style="color: red">1</span>, right);</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  64:  </span>}</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  65:  </span> </pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  66:  </span><span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> Swap (T[] array, <span style="color: #0000ff">int</span> i, <span style="color: #0000ff">int</span> k)</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  67:  </span>{</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  68:  </span>  T swap = array[i];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  69:  </span>  array[i] = array[k];</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #ffffff"><span style="color: #606060">  70:  </span>  array[k] = swap;</pre>
<pre style="font-size: small; margin: 0em; width: 100%; color: black; font-family: Consolas, 'Courier New', Courier, Monospace; background-color: #f4f4f4"><span style="color: #606060">  71:  </span>}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pherg.net/2007/12/10/c-quicksort/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Quicksort, Heapsort, and Introsort</title>
		<link>http://www.pherg.net/2007/12/05/quicksort-heapsort-and-introsort/</link>
		<comments>http://www.pherg.net/2007/12/05/quicksort-heapsort-and-introsort/#comments</comments>
		<pubDate>Wed, 05 Dec 2007 17:21:12 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://www.pherg.net/2007/12/05/quicksort-heapsort-and-introsort/</guid>
		<description><![CDATA[Ever heard of the introspective sort, also known as the introsort algorithm? The Wikipedia page is lacking, but this paper by David Musser describing the algorithm describes it in all its beauty. Basically it is an algorithm that sorts a list of elements by using a combination of Quicksort (with median of 3 and insertion [...]]]></description>
			<content:encoded><![CDATA[<p>Ever heard of the introspective sort, also known as the introsort algorithm? The <a target="_blank" href="http://en.wikipedia.org/wiki/Introsort">Wikipedia page</a> is lacking, but <a target="_blank" href="http://www.cs.rpi.edu/~musser/gp/introsort.ps">this paper</a> by David Musser describing the algorithm describes it in all its beauty. Basically it is an algorithm that sorts a list of elements by using a combination of <a target="_blank" href="http://en.wikipedia.org/wiki/Quicksort">Quicksort</a> (with median of 3 and insertion sort enhancements) and <a target="_blank" href="http://en.wikipedia.org/wiki/Heapsort">Heapsort</a>; Quicksort is used until a certain number of recursions has occured, whereupon the heapsort takes over.</p>
<p>Over the next few days I will present C# implementations of the Quicksort, Heapsort, and Introsort algorithms. In the meantime, go read David Musser&#8217;s paper.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pherg.net/2007/12/05/quicksort-heapsort-and-introsort/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Unbiased Shuffle Algorithm</title>
		<link>http://www.pherg.net/2007/11/29/unbiased-shuffle-algorithm/</link>
		<comments>http://www.pherg.net/2007/11/29/unbiased-shuffle-algorithm/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 22:29:14 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.pherg.net/?p=4</guid>
		<description><![CDATA[I have recently been fascinated by the mathematics of shuffling, or randomly reassigning values to different indices within an array of elements. Wikipedia has a good article about the Fisher-Yates shuffle, which, &#8220;properly implemented, &#8230; is unbiased, so that every permutation is equally likely.&#8221; Here is a C# implementation of the Fisher-Yates shuffle that runs [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently been fascinated by the mathematics of shuffling, or randomly reassigning values to different indices within an array of elements. Wikipedia has a good article about the <a href="http://en.wikipedia.org/wiki/Fisher-Yates_shuffle" target="_blank">Fisher-Yates shuffle</a>, which, &#8220;properly implemented, &#8230; is unbiased, so that every permutation is equally likely.&#8221; Here is a C# implementation of the Fisher-Yates shuffle that runs in linear, or O(n), time:</p>
<pre name="code" class="csharp">private void Shuffle (ref string[] values)
{
  Random rand = new Random(); // used to generate random positive integers
  int n = values.Length;      // length of the array
  int r = 0;                  // swap index
  string swap = String.Empty; // copy of string to swap

  // iterate through each item in the array
  for (int i = 0; i &lt; n; i++)
  {
    // get random index of an item to swap with the current item
    r = i + rand.Next(n - i);

    // swap items
    swap = values[r];
    values[r] = values[i];
    values[i] = swap;
  }
}</pre>
<p>You&#8217;ll notice that I&#8217;m sending in the array of strings as a reference argument instead of making a copy of the array and returning a new shuffled copy as the return type (which would be extremely inefficient).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pherg.net/2007/11/29/unbiased-shuffle-algorithm/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Mac OS X Time Machine with Linux Server</title>
		<link>http://www.pherg.net/2007/11/29/mac-os-105-time-machine-with-linux-server/</link>
		<comments>http://www.pherg.net/2007/11/29/mac-os-105-time-machine-with-linux-server/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 14:28:36 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[Leopard]]></category>

		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://blog.pherg.net/?p=3</guid>
		<description><![CDATA[Time Machine was one of those features that pushed me over the edge to buy the mac. The one thing that was a little disappointing is that it only works with a locally-connected drive (by default). This behavior, however, can be changed. I used Appletalk on my Linux box, but I&#8217;ve heard of people using [...]]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://www.apple.com/macosx/features/timemachine.html">Time Machine</a> was one of those features that pushed me over the edge to buy the mac. The one thing that was a little disappointing is that it only works with a locally-connected drive (by default). This behavior, however, can be changed. I used Appletalk on my Linux box, but I&#8217;ve heard of people using Samba (SMB) and NFS. Here&#8217;s how to set it up using Appletalk:</p>
<ol>
<li>Install <a target="_blank" href="http://netatalk.sourceforge.net/">netatalk</a> with ssh enabled on your Linux box (instructions for doing this on Ubuntu are found <a target="_blank" href="http://www.damontimm.com/blog/how-to-install-netatalk-afp-on-ubuntu-with-encrypted-authentication">here</a>).</li>
<li>Create a new directory to use as your Time Machine vault.<br />
<code>sudo mkdir /var/timemachine</code></li>
<li>Edit /etc/netatalk/AppleVolumes.default to add your AFP network drive (man page for this file is found <a target="_blank" href="http://netatalk.sourceforge.net/2.0/htmldocs/AppleVolumes.default.5.html">at this link</a>). At the bottom you will find the entries that define the Appletalk mount point and the &#8220;friendly name&#8221;. Add the following line at the end of the file to &#8220;mount&#8221; your new timemachine directory:<br />
<code>/var/timemachine/ "Time Machine"</code></li>
<li>Now, back on the Mac, select Finder&#8217;s &#8220;Go-&gt;Connect To Server&#8221; menu option. Enter the Appletalk address of your Linux box. For example, if my Linux box was at IP address 192.168.1.2, I would use this connection string:<br />
<code>afp://192.168.1.2</code><br />
If it prompts you for a username and password, use one that has read and write access to the /var/timemachine/ directory. I have found it easiest to just add a user to Linux that matches the username/password of a user on the Mac.</li>
<li>Select &#8220;Time Machine&#8221; as the drive to mount.</li>
<li>You need to tell Time Machine that it&#8217;s OK to use network drives for a backup volume by executing the following from a Mac Terminal shell:<br />
<code>defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1</code></li>
<li>Open Time Machine, select your new &#8220;Time Machine&#8221; network drive, and off you go.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.pherg.net/2007/11/29/mac-os-105-time-machine-with-linux-server/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
