【发布时间】:2014-01-27 21:56:08
【问题描述】:
我有一个点数组,与我的 dataID 数组相匹配。
points = [[2,3.5], [1,2.7], [5,2.21], [2,351]];
dataID = [1, 2, 3, 4];
我在 javaScript 中使用的快速排序函数是: (使用我自己的比较函数,按 X 然后 Y 进行比较,并且有效)
quicksortbyXthenY: function(array){
if(array.length <= 1) return array;
var pivot = array.splice(Math.round(array.length/2),1)[0];
var lower = greater = [];
jQuery.each(array, function() {
if(compare(this, pivot)>=0){
lower.push(this);
}else{
greater.push(this);
}
});
return (quicksortbyXthenY(lower).concat([pivot])).concat(quicksortbyXthenY(greater));
我需要知道点的新顺序,即与新的点顺序正确匹配的 dataID 的更新数组。
最简单的实现方法是什么?
【问题讨论】:
-
为什么使用两个单独的数组而不是单个对象数组?也许是这样的:
[{id:1,p:[2,3.5]},{id:2,p:[1,2.7]},...]- 或[{id:1,x:2,y:3.5},...]。 -
为什么要自己实现快速排序而不是使用原生的
sort方法?您的效率似乎要低得多。我也不明白byXthenY是什么意思。
标签: javascript sorting quicksort