获取数组中第n个最小的数字位置

时间:2013-04-02 00:39:13

标签: javascript jquery arrays algorithm sorting

非常基本的问题,但我似乎找不到任何如何在Javascript中解决它的例子。

我想创建一个函数,你传递一个代表“n”的数字,它返回数组中第n个最小数字的位置。

例如,如果我这样做:

array = [5,6,1,1,1,8]
n = 3
location = nth_smallest(array, n)

位置将等于4,因为第三个最小数字是1但是我想跳过该数字的前几个副本。

找到第n个最小数字位置的常见解决方案是:

array = [5,6,1,1,1,8]
n = 3
nth_lowest = array.slice(0).sort()[n]
location = $.inArray(nth_lowest, array)

然而问题是它总是返回2的位置,因为它知道第三个最小的数字是1,但inArray函数不关心重复。

有没有办法做到这一点,可能没有使用sort函数?它似乎占用了大量的处理,这是一个经常运行的功能。

2 个答案:

答案 0 :(得分:1)

// remap array as pairs of value and index
// e.g. change [5, 6, 1] to [[5, 0], [6, 1], [1, 2]]
var augmented_array = array.map(function(val, index) { return [val, index]; });
// sort pairs by the first position, breaking ties by the second
augmented_array.sort(function(a, b) {
    var ret = a[0] - b[0];
    if (ret == 0) ret = a[1] - b[1];
    return ret;
});
// example array will now be [[1, 2], [5, 0], [6, 1]]
// so we get the location by just looking at the second position of a pair
var location = augmented_array[n - 1][1];

如果您希望最后一个位置具有该值,请在排序后执行:

var position = n - 1;
while (position < augmented_array.length - 1 &&
       augmented_array[position][0] == augmented_array[position + 1][0]) {
  ++position;
}
var location = augmented_array[position][1];

或者,如果您想要第一个位置,请执行:

var position = n - 1;
while (position > 0 &&
       augmented_array[position][0] == augmented_array[position - 1][0]) {
  --position;
}
var location = augmented_array[position][1];

当然,lastIndexOfindexOf,正如其他一个答案所建议的那样,代码会减少。

答案 1 :(得分:0)

如果我正确理解您的问题,您正在寻找第n个最低数字的 last 实例的位置?如果是这样,试试这个:

array = [5,6,1,1,1,8];
n = 3;
nth_smallest = array.slice(0).sort()[n];
location = array.lastIndexOf(nth_smallest); // assumes non-ancient browser and/or shim

lastIndexOf的haxy垫片可以这样做:

function lastIndexOf(array,item) {
    return array.join("\x00").match(new RegExp(".*\x00"+item+"\x00"))[0].split("\x00").length-1;
}

这个垫片需要这样调用:location = lastIndexOf(array,nth_smallest);