<?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>Maxima and minima &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/maxima-and-minima/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: Minimum and Maximum</title>
		<link>/2012/05/21/computer-algorithms-minimum-and-maximum/</link>
		<comments>/2012/05/21/computer-algorithms-minimum-and-maximum/#comments</comments>
		<pubDate>Mon, 21 May 2012 20:14:30 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[Application This algorithm]]></category>
		<category><![CDATA[Calculus]]></category>
		<category><![CDATA[comparisons solution]]></category>
		<category><![CDATA[Counting sort]]></category>
		<category><![CDATA[Mathematical analysis]]></category>
		<category><![CDATA[Mathematical optimization]]></category>
		<category><![CDATA[Maxima and minima]]></category>
		<category><![CDATA[memory solution]]></category>
		<category><![CDATA[Minima and maxima]]></category>
		<category><![CDATA[Selection algorithm]]></category>
		<category><![CDATA[sequential search]]></category>
		<category><![CDATA[Sorting algorithms]]></category>
		<category><![CDATA[The algorithm]]></category>

		<guid isPermaLink="false">/?p=3134</guid>
		<description><![CDATA[Introduction To find the minimum value into an array of items itsn&#8217;t difficult. There are not many options to do that. The most natural approach is to take the first item and to compare its value against the values of all other elements. Once we find a smaller element we continue the comparisons with its &#8230; <a href="/2012/05/21/computer-algorithms-minimum-and-maximum/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Minimum and Maximum</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/05/28/computer-algorithms-order-statistics-the-algorithm/" rel="bookmark" title="Computer Algorithms: Order Statistics">Computer Algorithms: Order Statistics </a></li>
<li><a href="/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree">Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree </a></li>
<li><a href="/2012/02/13/computer-algorithms-insertion-sort/" rel="bookmark" title="Computer Algorithms: Insertion Sort">Computer Algorithms: Insertion Sort </a></li>
<li><a href="/2012/11/19/computer-algorithms-prims-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Prim&#8217;s Minimum Spanning Tree">Computer Algorithms: Prim&#8217;s Minimum Spanning Tree </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>To find the minimum value into an array of items itsn&#8217;t difficult. There are not many options to do that. The most natural approach is to take the first item and to compare its value against the values of all other elements. Once we find a smaller element we continue the comparisons with its value. Finally we find the minimum.</p>
<p><a href="/wp-content/uploads/2012/05/1.-Find-a-Minimum.png"><img class="size-full wp-image-3150" title="Find a Minimum" src="/wp-content/uploads/2012/05/1.-Find-a-Minimum.png" alt="Find a Minimum" width="621" height="431" srcset="/wp-content/uploads/2012/05/1.-Find-a-Minimum.png 621w, /wp-content/uploads/2012/05/1.-Find-a-Minimum-300x208.png 300w" sizes="(max-width: 621px) 100vw, 621px" /></a></p>
<p>First thing to note is that we pass through the array with <strong>n</strong> steps and we need exactly <strong>n-1</strong> comparisons. It’s clear that this is the optimal solution, because we must check all the elements. For sure we can’t be sure that we’ve found the minimum (maximum) value without checking every single value.<br />
<span id="more-3134"></span></p>
<h2>Overview</h2>
<p>The algorithm above is very simple and we’re sure that it is optimal. Obviously finding both the minimum and the maximum value is O(n) with <strong>n-1</strong> comparisons, but what about combining these tasks into one single pass.</p>
<figure id="attachment_3153" style="width: 621px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/05/2.-Find-a-Maximum.png"><img class="size-full wp-image-3153" title="Find a Maximum" src="/wp-content/uploads/2012/05/2.-Find-a-Maximum.png" alt="Find a Maximum" width="621" height="431" srcset="/wp-content/uploads/2012/05/2.-Find-a-Maximum.png 621w, /wp-content/uploads/2012/05/2.-Find-a-Maximum-300x208.png 300w" sizes="(max-width: 621px) 100vw, 621px" /></a><figcaption class="wp-caption-text">Finding the maximum is identical to finding the minimum and requires n-1 comparisons!</figcaption></figure>
<p>Since they both are <strong>O(n)</strong> and need <strong>n-1</strong> comparisons it’s natural to think that combining the two tasks will be O(n) and 2n &#8211; 2 comparisons. However we can reduce the number of comparisons!</p>
<p>Instead of taking only one item from the array and comparing it against the minimum and maximum we can take a pair of items at each step. Thus we can first compare them and then compare the smaller value with the currently smallest value and the greater item with the currently greatest value. This will make only 3 comparisons instead of 4.</p>
<p><a href="/wp-content/uploads/2012/05/3.-Find-both-minimum-and-maximum.png"><img class="alignnone size-full wp-image-3155" title="Find both minimum and maximum" src="/wp-content/uploads/2012/05/3.-Find-both-minimum-and-maximum.png" alt="Both minimum and maximum with less comparisons!" width="619" height="383" srcset="/wp-content/uploads/2012/05/3.-Find-both-minimum-and-maximum.png 619w, /wp-content/uploads/2012/05/3.-Find-both-minimum-and-maximum-300x185.png 300w" sizes="(max-width: 619px) 100vw, 619px" /></a></p>
<h2>Implementation</h2>
<p>It’s easy to implement the minimum (maximum) algorithms with a single loop.</p>
<p><script src="https://gist.github.com/stoimen/d2d44986bb70a19bc72c.js"></script></p>
<p>The implementation of finding the maximum is practically the same.</p>
<p><script src="https://gist.github.com/stoimen/fff5cb54c413ca332ffb.js"></script></p>
<p>Simply merging these two functions will lead us to a O(n) with 2n &#8211; 2 comparisons solution.</p>
<p><script src="https://gist.github.com/stoimen/82e563992421dc612498.js"></script></p>
<p>However we can take a pair of items on each step. First we’ll compare the items from that pair and after that we’ll compare them respectively with the minimum and the maximum value. Because on each iteration we jump by two items, in case the number of array items is even we must check for the array boundaries. This can be overcome by adding a sentinel. Thus the array items are always odd, but this will lead us to a &#8220;extra&#8221; memory solution.</p>
<h3>Sentinel</h3>
<p><script src="https://gist.github.com/stoimen/4b46f015c096630cd2b1.js"></script></p>
<h3>Without sentinel</h3>
<p><script src="https://gist.github.com/stoimen/a64ac6100e95f63812dc.js"></script></p>
<h2>Complexity</h2>
<p>The complexity of finding both minimum and maximum is O(n). Even after combining the both algorithms in one single pass the complexity remains O(n). However in the second case we can reduce the number of comparisons to 3 * ceil(n/2) instead of 2n &#8211; 2!</p>
<h2>Application</h2>
<p>This algorithm can be applied in various fields of the computer science, since its nature is so basic. However there are two reasons why this approach is so important.</p>
<p>First we can see how by combining two &#8220;algorithms&#8221; doesn’t mean that we combine their complexities or the number of operations. With a clever trick and with the observation that the two operations are related (minimum and maximum) we can reduce the number of comparisons.</p>
<p>In the other hand we see how using a sentinel can be very handy and can spare us some comparisons, just like the <a title="Computer Algorithms: Sequential Search" href="/2011/11/24/computer-algorithms-sequential-search/">sequential search</a>.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/05/28/computer-algorithms-order-statistics-the-algorithm/" rel="bookmark" title="Computer Algorithms: Order Statistics">Computer Algorithms: Order Statistics </a></li>
<li><a href="/2012/11/12/computer-algorithms-kruskals-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree">Computer Algorithms: Kruskal&#8217;s Minimum Spanning Tree </a></li>
<li><a href="/2012/02/13/computer-algorithms-insertion-sort/" rel="bookmark" title="Computer Algorithms: Insertion Sort">Computer Algorithms: Insertion Sort </a></li>
<li><a href="/2012/11/19/computer-algorithms-prims-minimum-spanning-tree/" rel="bookmark" title="Computer Algorithms: Prim&#8217;s Minimum Spanning Tree">Computer Algorithms: Prim&#8217;s Minimum Spanning Tree </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/05/21/computer-algorithms-minimum-and-maximum/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
