Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

algorithm - Sorting an array with minimal number of comparisons

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Donald Knuth's The Art of Computer Programming, volume 3 has a section on exactly this topic. I don't have the books here with me, but I'm pretty sure Knuth presents the algorithm for 5 elements. As you suspect, there isn't a general algorithm that gives the minimal number of comparisons for many sizes, but there are a number of common tricks that are used in such algorithms.

From vague recollections, I reconstructed the algorithm for 5 elements, and it can be done in 7 comparisons. First, take two separate pairs, compare inside those, and compare the smaller ones of each pair. Then, compare the remaining one against the larger of these. This now splits into two cases according to whether the remaining element was smaller or larger, but in all cases it's possible to finish in the three comparisons still available.

I recommend drawing pictures to help you. Knuth's pictures are something like this:

   o---o
  /
 o---o

which shows the results after the first three comparisons (and from what I recall, this kind of a picture appears in many minimal-comparison sorts). A line connects two elements whose order we know. Having such pictures helps you in determining which elements to compare against, as you want to make a comparison that gives you the maximal amount of information.

Addendum: Since there is an accepted answer with actual code, I guess there's no harm in finishing up these diagrams, and they might be a useful addition to the answer. So, start with the above one, and compare the missing element against the one on the top left. If it is larger, this will lead to

    /--o
   o
  / --o
 o
  --o

Now, compare the two large elements at the top right and we end up with

   o---o---o
  /
 o---o

Now, by comparing the bottom right element first against the middle one on top and then against whichever side it belongs on, we place it correctly using the remaining two comparisons.

If the initial comparison resulted in the remaining element being smaller, the diagram becomes

 o---o---o
    /
   o---o

Now, compare the two that have yet nothing smaller than them. One option is the last diagram above, which is solvable with the remaining two comparisons. The other case is

       o---o
      /
 o---o---o

And here again, the one that is not yet in sequence with the others can be placed correctly with two comparisons.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...