<?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-35548135</id><updated>2011-12-14T18:40:57.905-08:00</updated><title type='text'>Musings on Technology</title><subtitle type='html'>Random writings about programming and technological things. Mostly about stuff that strikes my fancy. I write a new post about once a month.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://richieb-on-tech.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/35548135/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://richieb-on-tech.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>richieb</name><uri>http://www.blogger.com/profile/14747695901068412710</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='24' src='http://photos-004.ak.facebook.com/ip002/v67/22/113/502286907/n502286907_46004_1307.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-35548135.post-116760701312561099</id><published>2006-12-31T14:58:00.000-08:00</published><updated>2007-01-01T06:50:29.780-08:00</updated><title type='text'>The worst sort algorithm in the world!</title><content type='html'>In interviewing candidates for programming jobs we have tried to come up with some unusual technical questions. Mostly this is to observe the person's reaction to such a question and to see how they might try to solve the problem.&lt;br /&gt;&lt;br /&gt;My latest favorite question in this vein is this:  "What is the worst sort algorithm you can think of?"  The more precise problem definition is that the algorithm must be guaranteed to terminate and that the input will be sorted. Typically bad sort is a bubble sort - whose execution complexity is O(N^2) in the worst case.&lt;br /&gt;&lt;br /&gt;But can you do better? Or rather, can you do worse?!&lt;br /&gt;&lt;br /&gt;Here is an algorithm that in the worse case is O(N!) - where N! stands for N factorial (i.e. N*(N-1)*(N-2)...2*1).  It works like this:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Take random permutation from the set of permutations of N elements&lt;/li&gt;&lt;li&gt;Apply this permutation to the input&lt;/li&gt;&lt;li&gt;If not yet sorted, choose another permutation and go back to previous step&lt;/li&gt;&lt;/ul&gt;Since there are N! permutations of N elements, on average N!/2 permutations will have to be tried. So the execution complexity of this algorithm will be O(N!).&lt;br /&gt;&lt;br /&gt;Just to try it out I wrote this sort in Ruby. Here is the code:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;require "permutation"&lt;br /&gt;&lt;br /&gt;class Array&lt;br /&gt;  # return true if array is sorted&lt;br /&gt;  def sorted?&lt;br /&gt;    return true if self.length &lt;= 1      &lt;br /&gt;    for i in 0..self.length-2         &lt;br /&gt;      return false if (self[i] &gt; self[i+1])&lt;br /&gt;    end&lt;br /&gt;    true&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def worst_sort (a)&lt;br /&gt;   perm = Permutation.new(a.length)&lt;br /&gt;   while (perm.rank != perm.last)&lt;br /&gt;      sa = perm.project(a)&lt;br /&gt;      return sa if (sa.sorted?)&lt;br /&gt;      perm.succ!&lt;br /&gt;   end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I tried running this sort on my laptop and for an array of 11 elements it was taking so long that I lost patience and stopped the program.&lt;br /&gt;&lt;br /&gt;If you look at the code above the most difficult part is generating permutations on N elements in order. I was lucky to find that the hard work was already done for me in the Ruby "permutations" module. You can find the source for this module here: &lt;a href="http://permutaion.rubyforge.org"&gt;http://permutation.rubyforge.org.&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/35548135-116760701312561099?l=richieb-on-tech.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://richieb-on-tech.blogspot.com/feeds/116760701312561099/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=35548135&amp;postID=116760701312561099' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/35548135/posts/default/116760701312561099'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/35548135/posts/default/116760701312561099'/><link rel='alternate' type='text/html' href='http://richieb-on-tech.blogspot.com/2006/12/worst-sort-algorithm-in-world.html' title='The worst sort algorithm in the world!'/><author><name>richieb</name><uri>http://www.blogger.com/profile/14747695901068412710</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='24' src='http://photos-004.ak.facebook.com/ip002/v67/22/113/502286907/n502286907_46004_1307.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-35548135.post-116493496554477527</id><published>2006-11-30T16:55:00.000-08:00</published><updated>2006-12-01T05:01:48.586-08:00</updated><title type='text'>Do you remember your first program?</title><content type='html'>The other day we were sitting at lunch reminiscing about how we got started  in programming. Every person present had a different story. Here is the story  of my first program.&lt;br /&gt;&lt;br /&gt;It was the mid-seventies, also known as the Jurasic era of computing, and I was in college taking a physics class. I needed science courses for my future B.S. (Bachelors of Science) degree, so I took physics because I wanted to avoid chemistry. In the physics class we did occsional labs and our professor wanted us to write a computer program to process lab results.&lt;br /&gt;&lt;br /&gt;Just to set the stage for the computing technology at the time remember there were no PCs. I had just gotten my first calculator, wich cost $40, not a small sum in the 70s. The calculator had a red LED display and only provided four operations: +, -, * and /. No square root. In fact I learned to use Newton's method to extract square roots by hand. Mind you, my physics  professor could do computations and estimates in his head faster than I could  type them on my calculator.&lt;br /&gt;&lt;br /&gt;In any case after one of the labs, he wrote some gibbrish on the board  (starting with "//JOB ") and told us to get some punch cards, and go to the computing center and write and run a program. The program in question was to read seven numbers from its input and print the average. The programming language we were to use was called FORTRAN.&lt;br /&gt;&lt;br /&gt;Step one was to buy blank punch cards at the college book store. They were sold in batches of 100. The I had to find the compuer center with the keypunch machines. A kepunch machine was a desk size thing with a typewriter keyboard and an elaborate mechanism for feeding in blank cards. Each letter you typed was encoded in a column of what seem like random holes in the card. If your particular machine had decent printing ribbon in place the character was also shown on the top. The funny thing was that it was hard to see what it was you were typing, so it was easy to make a mistake. Of course, you couldn't fix the card - can't fill in the holes - so a card was wasted and you had to start over.&lt;br /&gt;&lt;br /&gt;Once you keypunched you program and the magical control cards that went in front of it, you brought your deck to the RJE - Remote Job Entry - station and handed your card deck though a window to a student employed at the  computer center to be read in. He or she would do it immediately and hand you your deck back. The computer would attempt to run job later. The output, a  folded print out, would be put on some shelves when it eventually came out.&lt;br /&gt;&lt;br /&gt;Nobody told me that if you make a mistake in the control cards (&lt;a href="Jhttp://en.wikipedia.org/wiki/JCL"&gt;JCL&lt;/a&gt; JOB card for you old timers) no output will be generated. So after having my deck read in I hanged out around the computer center wating for results. When nothig came out after what seemed like a reasonable time, I asked for help from one of the local "hackers". He quickly identified my JCL error and told me how to fix it.&lt;br /&gt;&lt;br /&gt;This eventually worked and I got my first program to run and print out a weird looking answer (things like: 000102E02 or some such).&lt;br /&gt;&lt;br /&gt;So, what was your first program?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/35548135-116493496554477527?l=richieb-on-tech.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://richieb-on-tech.blogspot.com/feeds/116493496554477527/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=35548135&amp;postID=116493496554477527' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/35548135/posts/default/116493496554477527'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/35548135/posts/default/116493496554477527'/><link rel='alternate' type='text/html' href='http://richieb-on-tech.blogspot.com/2006/11/do-you-remember-your-first-program.html' title='Do you remember your first program?'/><author><name>richieb</name><uri>http://www.blogger.com/profile/14747695901068412710</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='24' src='http://photos-004.ak.facebook.com/ip002/v67/22/113/502286907/n502286907_46004_1307.jpg'/></author><thr:total>5</thr:total></entry></feed>
