<?xml version="1.0" encoding="UTF-8"?><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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>then search &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/then-search/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Order Statistics</title>
		<link>/2012/05/28/computer-algorithms-order-statistics-the-algorithm/</link>
		<comments>/2012/05/28/computer-algorithms-order-statistics-the-algorithm/#respond</comments>
		<pubDate>Mon, 28 May 2012 19:37:00 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Best worst and average case]]></category>
		<category><![CDATA[Bubble sort]]></category>
		<category><![CDATA[Merge sort]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Quicksort]]></category>
		<category><![CDATA[Selection algorithm]]></category>
		<category><![CDATA[Sorting algorithms]]></category>
		<category><![CDATA[then search]]></category>

		<guid isPermaLink="false">/?p=3149</guid>
		<description><![CDATA[Introduction We know that finding the minimum in a list of integers is a fairly simple task, but what about finding the i-th smallest element? Then the task isn’t that trivial and we have to think for a different approach. First of all there are some very basic and intuitive approaches. Since finding the minimum &#8230; <a href="/2012/05/28/computer-algorithms-order-statistics-the-algorithm/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Order Statistics</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/03/13/computer-algorithms-quicksort/" rel="bookmark" title="Computer Algorithms: Quicksort">Computer Algorithms: Quicksort </a></li>
<li><a href="/2012/03/05/computer-algorithms-merge-sort/" rel="bookmark" title="Computer Algorithms: Merge Sort">Computer Algorithms: Merge Sort </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2012/02/20/computer-algorithms-bubble-sort/" rel="bookmark" title="Computer Algorithms: Bubble Sort">Computer Algorithms: Bubble Sort </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>We know that <a href="/2012/05/21/computer-algorithms-minimum-and-maximum/" title="Computer Algorithms: Minimum and Maximum">finding the minimum in a list of integers</a> is a fairly simple task, but what about finding the i-th smallest element? Then the task isn’t that trivial and we have to think for a different approach. </p>
<p>First of all there are some very basic and intuitive approaches. Since finding the minimum is so easy, can we just find the minimum, than exclude it from the list and then search the minimum again until we find the i-th smallest element.</p>
<p><figure id="attachment_3164" style="width: 621px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/05/1.-Finding-the-Minimums.png"><img src="/wp-content/uploads/2012/05/1.-Finding-the-Minimums.png" alt="Finding the Minimums" title="Finding the Minimums" width="621" height="302" class="size-full wp-image-3164" srcset="/wp-content/uploads/2012/05/1.-Finding-the-Minimums.png 621w, /wp-content/uploads/2012/05/1.-Finding-the-Minimums-300x145.png 300w" sizes="(max-width: 621px) 100vw, 621px" /></a><figcaption class="wp-caption-text"> </figcaption></figure><span id="more-3149"></span></p>
<p>That is a pure brute-force-like algorithm and it is extremely slow. In this case if we’re looking for the 99-th smallest element into an array of 100 items it will be quite inefficient. In other words this isn’t the best approach.</p>
<p>Another fairly intuitive approach is to sort the list in first place and then search the i-th element. </p>
<figure id="attachment_3163" style="width: 621px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/05/2.-Sort-and-Seach.png"><img src="/wp-content/uploads/2012/05/2.-Sort-and-Seach.png" alt="Sort and Seach" title="Sort and Seach" width="621" height="299" class="size-full wp-image-3163" srcset="/wp-content/uploads/2012/05/2.-Sort-and-Seach.png 621w, /wp-content/uploads/2012/05/2.-Sort-and-Seach-300x144.png 300w" sizes="(max-width: 621px) 100vw, 621px" /></a><figcaption class="wp-caption-text">First we can sort the list and then search for the i-th element!</figcaption></figure>
<p>This is better than the our first attempt because we&#8217;ll need the time to sort the array and then search (in linear time) the i-th element.</p>
<p>In this case we need to find out which of the sorting algorithms we will use. Will it be <a href="/2012/03/05/computer-algorithms-merge-sort/" title="Computer Algorithms: Merge Sort">merge sort</a> (with constant O(n.lg(n)) complexity) or <a href="/2012/03/13/computer-algorithms-quicksort/" title="Computer Algorithms: Quicksort">quicksort</a> (with O(n<sup>2</sup>) in the worst case, but O(n.lg(n)) average complexity) or <a href="/2012/02/20/computer-algorithms-bubble-sort/" title="Computer Algorithms: Bubble Sort">bubble sort</a> (O(n^n) in the best-case scenario) it’s a developer choice.</p>
<p>However there is one very clever and yet more efficient approach, based on some observations.</p>
<h2>Overview</h2>
<p>If we’re looking for the i-th element and we decided that the list must be sorted first, we don’t need to fully sort it in order to find the desired element. </p>
<p>In case the list is sorted it’s easy to find which is the i-th element. However if the i-th element is in its place, the only thing we need to know is that the items on the left side of the i-th element are smaller and the items on the right side are greater. We don’t need the left and the right side ordered.</p>
<figure id="attachment_3162" style="width: 619px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/05/3.-Dont-need-ordered-sub-lists.png"><img src="/wp-content/uploads/2012/05/3.-Dont-need-ordered-sub-lists.png" alt="Don&#039;t need ordered sub-lists" title="Don&#039;t need ordered sub-lists" width="619" height="279" class="size-full wp-image-3162" srcset="/wp-content/uploads/2012/05/3.-Dont-need-ordered-sub-lists.png 619w, /wp-content/uploads/2012/05/3.-Dont-need-ordered-sub-lists-300x135.png 300w" sizes="(max-width: 619px) 100vw, 619px" /></a><figcaption class="wp-caption-text"> </figcaption></figure>
<p>In the other hand this approach looks very much like quicksort. There during the sorting process we put the items smaller than the “pivot” on its left and the items greater than the pivot on its right. After that partitioning we executed quicksort on the left and on the right sub-lists.</p>
<p>Here the approach is similar with very small changes. First we choose a pivot. Then we make two partitions of the list &#8211; one left sub-list with all the elements with smaller values than the pivot and one right sub-list with all the elements with a greater value than the pivot. </p>
<figure id="attachment_3161" style="width: 618px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/05/4.-Choose-a-pivot-and-partition.png"><img src="/wp-content/uploads/2012/05/4.-Choose-a-pivot-and-partition.png" alt="Choose a pivot and partition" title="Choose a pivot and partition" width="618" height="229" class="size-full wp-image-3161" srcset="/wp-content/uploads/2012/05/4.-Choose-a-pivot-and-partition.png 618w, /wp-content/uploads/2012/05/4.-Choose-a-pivot-and-partition-300x111.png 300w" sizes="(max-width: 618px) 100vw, 618px" /></a><figcaption class="wp-caption-text">Just like quicksort we chose a pivot and then we partition the list into two sub-lists!</figcaption></figure>
<p>Now we check the length of the left sub-list. If it is greater than i we continue recursively with the left sub-list and again we’re searching for the i-th element.</p>
<p>In case the length of the left sub-list is smaller than i, we continue with the right sub-list. However this time we don’t search for the i-th element, but for the i &#8211; length(LEFT). </p>
<h2>Implementation</h2>
<p>The following implementation is in <a href="/category/php/" title="PHP on stoimen.com">PHP</a>. It’s important to note that at each step we need two non-empty sub-list. That is why we take the pivot (by extracting the last item of the list) and then making two sub-lists. In case one of the sub-lists is empty we append the pivot in it. Thus we’re always partitioning the list into two non-empty sub-lists. </p>
<pre lang="PHP">
$list = array(3,4,5,7,8,2,5,6,9,0,1);

function partition($list, $pivot)
{
	$left = $right = array();
	
	$len = count($list);
	for ($i = 0; $i < $len; $i++) {
		if ($list[$i] <= $pivot) {
			$left[] = $list[$i];
		} else {
			$right[] = $list[$i];
		}
	}

	if (count($left) == 0) {
		$left[] = $pivot;
	} else {
		$right[] = $pivot;
	} 
	
	return array($left, $right);
}

function order_statistic($list, $i)
{
	if (count($list) == 1) {
		return $list[0];
	}
	
	// ceate a non empty partitions
	// extract the pivot from the list and
	// in case one of the sub-lists is empty
	// add the pivot there!
	$pivot = array_pop($list);
	list($left, $right) = partition($list, $pivot);
	
	if (count($left) >= $i) {
		return order_statistic($left, $i);
	} else {
		return order_statistic($right, $i - count($left));
	}
}

// 4
echo order_statistic($list, 5);
</pre>
<h2>Application</h2>
<p>Finding the minimum and maximum is easy, however sometimes we don&#8217;t search for them, but for the second, third or i-th smallest element. Then our task becomes a bit more difficult. This algorithm can be useful in many practical cases and shows us how different kind of algorithms may be related &#8211; exactly as this algorithm is related to quicksort in its principles.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/03/13/computer-algorithms-quicksort/" rel="bookmark" title="Computer Algorithms: Quicksort">Computer Algorithms: Quicksort </a></li>
<li><a href="/2012/03/05/computer-algorithms-merge-sort/" rel="bookmark" title="Computer Algorithms: Merge Sort">Computer Algorithms: Merge Sort </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2012/02/20/computer-algorithms-bubble-sort/" rel="bookmark" title="Computer Algorithms: Bubble Sort">Computer Algorithms: Bubble Sort </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/05/28/computer-algorithms-order-statistics-the-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
