Iterative Javascript QuickSelect

June 2012 · 2 minute read

A couple weeks ago I entered my first AI related contest, hosted by scribd. The contest was great for beginners since it involved a very simple world and control scheme.

At some point in by bot creation process I needed to determine the n-th highest number in an Array. While for such a simple project I probably should have just thrown some loops together and moved on, but since the project was just I started researching the problem and Hoare’s Selection Algorithm. I wasn’t able to find a Javascript implementation so I ported a Java version() which in turn had been ported over from the book “Numerical Recipes in C: The Art of Scientific Computing”. The best part about this implementation is that unlike most, it isn’t recursive. This makes it more verbose but much more interesting and it avoids any stack depth restrictions that browser vendors may impose.