Refactors code to simplify templating

I refactored the templates to use random-access iterator semantics; this
allows generic sorting on both STL containers and arrays, since pointers
are random-access iterators.