<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7156634666153484319</id><updated>2012-02-02T02:28:55.796-06:00</updated><category term='CompSci'/><category term='General'/><category term='C'/><title type='text'>comamitc</title><subtitle type='html'>{random}</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://comamitc.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156634666153484319/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://comamitc.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Mitch Comardo</name><uri>https://profiles.google.com/100123828465904517826</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-tXqa7Pz3CKo/AAAAAAAAAAI/AAAAAAAAAj8/4vMSqE6Frb0/s512-c/photo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7156634666153484319.post-7420681293509849600</id><published>2011-09-29T08:38:00.002-05:00</published><updated>2011-10-03T03:59:33.649-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='CompSci'/><title type='text'>Peak Finding</title><content type='html'>A few days ago I decided to run through some&amp;nbsp;exercises&amp;nbsp;in C (since I am teaching myself it) and analyze some of the common algorithms that are explored in general Comp Sci programs. &lt;a href="http://www.amazon.com/Introduction-Algorithms-Thomas-H-Cormen/dp/0262033844"&gt;&amp;nbsp;"Introduction to Algorithms"&lt;/a&gt; by Thomas Cormen, Charles Leiserson &amp;amp; Ronald Rivest &amp;nbsp;is pretty much the standard go-to book. &amp;nbsp;Coincidentally, a few months back I also found a post on Hacker News that was the class material for the MIT course that accompanies the book. &amp;nbsp;It helps that the algorithms have been translated to Python, although the code language shouldn't matter, it eases me translating it to C which I am fairly new at.&lt;br /&gt;&lt;br /&gt;Anyways, one of&amp;nbsp;the&amp;nbsp;first algorithms is peak finding (a sort of binary search algorithm that returns peaks). &amp;nbsp;That is, given an array on &lt;i&gt;n&lt;/i&gt; numbers what number is the largest? The final example the slides give is a peak function called peak1d which recursively splits the array into two halves and compares the elements around that midway point which should eventually yield the peak in the array. The code reads:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;peak1d(A, i, j):&amp;nbsp;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp;m = (i+j)/2&amp;nbsp;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp;if A[m-1] &amp;lt;= A[m] &amp;lt;= A[m+1]:&amp;nbsp;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; return m&amp;nbsp;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp;elif A[m-1] &amp;gt; A[m]:&amp;nbsp;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; return peak1d(A, i, m-1)&amp;nbsp;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp;elif A[m] &amp;lt; A[m+1]:&amp;nbsp;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; return peak1d(A, m+1, j)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The goal of this algorithm is to somewhat find everything considered a 'peak' within the array. That is it is a number that is higher than the numbers that reside either directly to the right or left of it. &lt;br /&gt;&lt;br /&gt;There are a few things that (I think) fall short in this code (having to split the array into two before sending it to the algorithm and then&amp;nbsp;comparing&amp;nbsp;the two halves, finding peaks but not necessarily THE PEAK, multiple peaks in the two halves, etc) but the biggest most interesting one I saw was handling the ends of the array. That is given an array with indexes 0,1,2,3,4,5; what if positions 0 or 5 hold the maximum number? &amp;nbsp;The array would be out of index when trying to compare the first if statement and if translated to C, the return of index A[6] would return garbage. &lt;br /&gt;&lt;br /&gt;So in exploring C, I though I would take a hack at modifying the algorithm to accept the ends at peaks and focus on the highest peaks rather than a series of them. &amp;nbsp;So I somewhat digress, because the above algorithm actually works exactly like intended by finding the peaks in an array and returning them but it seems somewhat impractical. Below is the C code I came up with:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;int peak(int a[], int s, int e){&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;	&lt;/span&gt;int l, r;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;	&lt;/span&gt;int d = e-s;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;	&lt;/span&gt;if(d &amp;gt; 1){&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;		&lt;/span&gt;// recursive clause for too many values to compare&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;		&lt;/span&gt;int m = (s + e) / 2;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;		&lt;/span&gt;l = peak(a, s, m-1);&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;		&lt;/span&gt;r = peak(a, m, e);&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;	&lt;/span&gt;} else if (d == 1){&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;		&lt;/span&gt;// compares two values when only 2 are sent&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;		&lt;/span&gt;l = a[s];&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;		&lt;/span&gt;r = a[e];&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;	&lt;/span&gt;} else if(d == 0){&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;		&lt;/span&gt;// handles singular values upon odd arrays&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;		&lt;/span&gt;return a[s];&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;	&lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;	&lt;/span&gt;return (l &amp;gt; r) ? l : r;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This algorithm takes the entire array along with the 0 index and the (length of the array - 1) to start and then recursively reduces the range of the array by half and finds the peak for each half. &amp;nbsp;This essentially boils down to returning the bigger of two specific values in the left half of the array and then comparing it to the largest value in the right half of the array. &amp;nbsp;It's an interesting algorithm and took some thought to catch the edges of the array and handle a dynamic array size whether it be even or odd. &amp;nbsp;In the end, the effort seems somewhat futile. If you do the math if the array is &lt;i&gt;n&lt;/i&gt; numbers long, then you have to still page through n&lt;i&gt;-1&lt;/i&gt; items in the array no matter what. &amp;nbsp;This is only one time better than if you where to apply a linear algorithm to such an array to find the peak where the paging length would be &lt;i&gt;n&lt;/i&gt; instead:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;int lpeak(int a[], int c){&lt;br /&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&amp;nbsp;   &lt;/span&gt;int m = 0, i;&lt;br /&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&amp;nbsp;   &lt;/span&gt;for (i = 0; i &amp;lt; c; i++){&lt;br /&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;	&lt;/span&gt;if (a[i] &amp;gt; m){&lt;br /&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;		&lt;/span&gt;m = a[i];&lt;br /&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;	&lt;/span&gt;}&lt;br /&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&amp;nbsp;   &lt;/span&gt;}&lt;br /&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&amp;nbsp;   &lt;/span&gt;return m;&lt;br /&gt;}&lt;/code&gt; &lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;What does that mean? &amp;nbsp;As &lt;i&gt;n&lt;/i&gt;&amp;nbsp;grows to say 1000, the reduction in calculation time of my "halves" algorithm diminishes, and the additional complexity of the code and general ugliness of it makes it something you would never want to use or maintain. In addition, the recursive nature of my solution also replicated the array &lt;i&gt;n-1 &lt;/i&gt;number of times, with which a large array would cause massive overhead.&lt;br /&gt;&lt;br /&gt;I guess there is not really a conclusion to this post other than analyzing my findings. &amp;nbsp;The main take away from it for me was one, have an end goal so that you solution isn't a digression of the original topic (mistake A I made) &amp;amp; two is that it just reiterates how simple, clean code allot of time ends up being better code too.&lt;br /&gt;&lt;br /&gt;Until next time, here's a photo of some empty London Guard Booths:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-AkVYPKarPO8/ToR0zAX59zI/AAAAAAAAAmo/neRdWPH8QUs/s1600/IMG_1453%255B1%255D.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://3.bp.blogspot.com/-AkVYPKarPO8/ToR0zAX59zI/AAAAAAAAAmo/neRdWPH8QUs/s320/IMG_1453%255B1%255D.JPG" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7156634666153484319-7420681293509849600?l=comamitc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://comamitc.blogspot.com/feeds/7420681293509849600/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://comamitc.blogspot.com/2011/09/peak-finding.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156634666153484319/posts/default/7420681293509849600'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156634666153484319/posts/default/7420681293509849600'/><link rel='alternate' type='text/html' href='http://comamitc.blogspot.com/2011/09/peak-finding.html' title='Peak Finding'/><author><name>Mitch Comardo</name><uri>https://profiles.google.com/100123828465904517826</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-tXqa7Pz3CKo/AAAAAAAAAAI/AAAAAAAAAj8/4vMSqE6Frb0/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-AkVYPKarPO8/ToR0zAX59zI/AAAAAAAAAmo/neRdWPH8QUs/s72-c/IMG_1453%255B1%255D.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7156634666153484319.post-2419628242566444289</id><published>2011-09-15T12:16:00.002-05:00</published><updated>2011-09-30T00:29:24.455-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='General'/><title type='text'>Again...</title><content type='html'>Hello World,&lt;br /&gt;&lt;br /&gt;Sorry for switching it up again. &amp;nbsp;Posterous dramatically complicated how to post on their platform so I decided to drop that and migrate back to the old comamitc blog... &amp;nbsp;this one has been alive since August 2006 when I moved to Midwestern State University in Wichita Falls, TX. Boy allot's changed since then.&lt;br /&gt;&lt;br /&gt;I don't really ride bikes anymore. &amp;nbsp;Well not even really. &amp;nbsp;I don't ride bikes anymore. &amp;nbsp;Not to say I don't want to but the last few Months haven't been very&amp;nbsp;conducive&amp;nbsp;to that seeing as I'm living in Germany and it just doesn't make a ton of sense to get deeply rooted in some place that isn't my home country.&lt;br /&gt;&lt;br /&gt;I think I worked in a bike shop last I posted as well. &amp;nbsp;After a few lucky career moves in my last few years of college I found myself in the IT industry and mostly centered around Oil and Gas (go figure with Houston and all). The&amp;nbsp;opportunities&amp;nbsp;have really helped me gain some traction in direction and for the first time in my life I feel like I can say, I don't want to do that... &amp;nbsp;Somewhat of the different&amp;nbsp;mentality&amp;nbsp;I previously had where just about everything interested me. &lt;br /&gt;&lt;br /&gt;I read a few books along the way, met a few people, had old friends make further impressions and eventually... well here I am. &amp;nbsp;I think there is probably just a little too much that's happened in the space that gaps my blog posts, so we will all just have to sit back, enjoy the ride, stay in touch and at the very least be&amp;nbsp;amused&amp;nbsp;with poor&amp;nbsp;grammar.&lt;br /&gt;&lt;br /&gt;I think I'll get in the habit of concluding posts with cool pictures. &amp;nbsp;Here is one of the most amazing person on the planet:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-T5zw_K0BEq4/TnIye8s-DNI/AAAAAAAAAmk/gVZgnDiZ6Gs/s1600/photo.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-T5zw_K0BEq4/TnIye8s-DNI/AAAAAAAAAmk/gVZgnDiZ6Gs/s1600/photo.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;~ Mitch&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7156634666153484319-2419628242566444289?l=comamitc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://comamitc.blogspot.com/feeds/2419628242566444289/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://comamitc.blogspot.com/2011/09/hello-world-sorry-for-switching-it-up.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7156634666153484319/posts/default/2419628242566444289'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7156634666153484319/posts/default/2419628242566444289'/><link rel='alternate' type='text/html' href='http://comamitc.blogspot.com/2011/09/hello-world-sorry-for-switching-it-up.html' title='Again...'/><author><name>Mitch Comardo</name><uri>https://profiles.google.com/100123828465904517826</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-tXqa7Pz3CKo/AAAAAAAAAAI/AAAAAAAAAj8/4vMSqE6Frb0/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-T5zw_K0BEq4/TnIye8s-DNI/AAAAAAAAAmk/gVZgnDiZ6Gs/s72-c/photo.JPG' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
