I need some help with my CS homework. I need to write a sorting routine that sorts an array of length 5 using 7 comparisons in the worst case (I’ve proven that 7 will be needed, because of the height of the decision tree).
I considered using the decision tree ‘hard-coded’, but that means the algorithm is really complicated and was hinted by my tutor that’s not the way it’s supposed to be done.
I’ve checked quicksort, merge sort, heap sort, d-ary heap sort, insertion sort, selection sort, all do not answer the requirement, which leads me to believe there’s a need for a specific algorithm for arrays of length 5.
Would really like to get some hints towards the right direction.
Yes it is in Knuth vol 3 page 185 (section 5.3.1). This was first documented in a PhD thesis so your Prof is being quite hard on you! There is no really simple elegant method; you have to track it as a partially ordered tree.
Here it is in lisp. Tested OK (SBCL on Linux)