Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a sort algorithm with as few as possible compare operations

I want to sort items where the comparison is performed by humans:

  • Pictures
  • Priority of work items
  • ...

For these tasks the number of comparisons is the limiting factor for performance.

  • What is the minimum number of comparisons needed (I assume > N for N items)?
  • Which algorithm guarantees this minimum number?
like image 759
gyrolf Avatar asked May 15 '09 06:05

gyrolf


People also ask

Which sorting algorithm uses fewest comparisons?

Merge-insertion sort is the sorting algorithm with the minimum possible comparisons for n items whenever n ≤ 15 or 20 ≤ n ≤ 22, and it has the fewest comparisons known for n ≤ 46. Glenn K.

How do you compare two sorting algorithms?

In a comparison based sorting algorithms, we compare elements of an array with each other to determines which of two elements should occur first in the final sorted list. All comparison-based sorting algorithms have a complexity lower bound of nlogn.

Which sorting algorithms are comparison based?

Some of the most well-known comparison sorts include: Quicksort. Heapsort. Shellsort.


1 Answers

To answer this, we need to make a lot of assumptions.

Let's assume we are sorting pictures by cuteness. The goal is to get the maximum usable information from the human in the least amount of time. This interaction will dominate all other computation, so it's the only one that counts.

As someone else mentioned, humans can deal well with ordering several items in one interaction. Let's say we can get eight items in relative order per round.

Each round introduces seven edges into a directed graph where the nodes are the pictures. If node A is reachable from node B, then node A is cuter than node B. Keep this graph in mind.

Now, let me tell you about a problem the Navy and the Air Force solve differently. They both want to get a group of people in height order and quickly. The Navy tells people to get in line, then if you're shorter than the guy in front of you, switch places, and repeat until done. In the worst case, it's N*N comparison.

The Air Force tells people to stand in a square grid. They shuffle front-to-back on sqrt(N) people, which means worst case sqrt(N)*sqrt(N) == N comparisons. However, the people are only sorted along one dimension. So therefore, the people face left, then do the same shuffle again. Now we're up to 2*N comparisons, and the sort is still imperfect but it's good enough for government work. There's a short corner, a tall corner opposite, and a clear diagonal height gradient.

You can see how the Air Force method gets results in less time if you don't care about perfection. You can also see how to get the perfection effectively. You already know that the very shortest and very longest men are in two corners. The second-shortest might be behind or beside the shortest, the third shortest might be behind or beside him. In general, someone's height rank is also his maximum possible Manhattan distance from the short corner.

Looking back at the graph analogy, the eight nodes to present each round are eight of those with the currently most common length of longest inbound path. The length of the longest inbound path also represents the node's minimum possible sorted rank.

You'll use a lot of CPU following this plan, but you will make the best possible use of your human resources.

like image 134
Ian Avatar answered Nov 03 '22 23:11

Ian