Context
I perform a psychological test in which the user is presented with pairs of images that should indicate which they prefer. They respond to their preference either with the A key or with L. If the number of images is large enough, then comparing all possible pairs requires the individual (O (n ^ 2)).
Question
I hacked a merge sort algorithm here to significantly 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;
}
getUserPreference . ( "" ~ 1/3 , "" , ), , -, :
, , - , whizzkids , , .