4

Context

I am implementing a psychological test whereby a user is presented with pairs of images an has to indicate which they prefer. They respond to their preference with either the A or L key. If the number of images is quite large then comparing all possible pairs is rather demanding upon the individual (O(n^2)).

Question

I have hacked the merge-sort algorithm here to dramatically reduce the number of comparisons. The result is the following:

function mergeSort(arr)
{
    if (arr.length < 2)
        return arr;

    var middle = parseInt(arr.length / 2);
    var left   = arr.slice(0, middle);
    var right  = arr.slice(middle, arr.length);

    return merge(mergeSort(left), mergeSort(right));
}


function merge(left, right)
{
    var result = [];

    while (left.length && right.length) {
        if (getUserPreference(left[0],right[0])) {
            result.push(left.shift());
        } else {
            result.push(right.shift());
        }
    }

    while (left.length)
        result.push(left.shift());

    while (right.length)
        result.push(right.shift());

    return result;
}

where the function getUserPreference delegates to the user. I have run multiple simulations (where the response can accidentally be "wrong" ~1/3 of the time, and by "wrong" I mean not input their true preference by hitting the wrong key) and the sort appears to perform well according to the following criteria:

  • Output results are close enough to the user preference (simulated).
  • The algorithm stops in good time. ~20 steps for a list of 10 items.

What I would like to know is if the algorithm whizzkids out there can tell me if there is any chance that this algorithm will completely fail to either stop or approximate the input solutions.

r3mainer
  • 23,981
  • 3
  • 51
  • 88
Joe
  • 1,455
  • 2
  • 19
  • 36
  • What's your desired output? -- a sorted list, the top N, the top one, or something else? – eh9 Oct 25 '16 at 15:38
  • Exactly. So the response now has a probability of error. Apologies if this was not clear. I reproduced the code as I understand link posting is not best practice. – Joe Oct 25 '16 at 15:40
  • The desired output is an approximately sorted list. This is what I get in all the simulations I have run. Just wanted to know about any theoretical limitations. Such as "this will fail if......" – Joe Oct 25 '16 at 15:41
  • 1
    Don't use `parseInt` on numbers. Use `Math.floor` instead. – Bergi Oct 25 '16 at 15:47
  • 2
    This question is ill-posed. You don't have a comparison with transitivity, you have an arbitrary relation matrix. You haven't defined what "approximately sorted" means. Without a mathematical model of what these mean, it's hard to know what might even work. And yes, you'll fail to terminate in some cases with any sort algorithm that assumes a total ordering. – eh9 Oct 25 '16 at 15:48
  • Do you have a reference for the fact that merge sort will fail to terminate without a total order? I am not observing that but keen to know if true. – Joe Oct 25 '16 at 15:51
  • Approximate sorted would mean in this case a position based distance measure. – Joe Oct 25 '16 at 15:52

1 Answers1

3

Is there any chance that this algorithm will completely fail to stop?

No. The algorithm always halts, no matter how bad/wrong/unfortunate the comparsisons are.

The algorithm stops in good time. ~20 steps for a list of 10 items.

For 10 items, the best case is 15 comparisons, the worst case is 25 comparisons. In general, merge sort takes on average O(n log n) steps.

Is there any chance that this algorithm will completely fail to approximate the input solutions

That depends a lot on your definition of "close enough to the user preference". And probably an important mistake would be to assume that user preference is a transitive relation at all.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375