简体   繁体   English

JavaScript Array.sort([compareFunc])的内部工作原理;

[英]The inner workings of JavaScript Array.sort([compareFunc]);

I'm trying very hard to "see" what's going on within a call to Array.sort() when passing a simple comparison function. 我正在努力通过简单的比较函数来“查看”对Array.sort()的调用中发生了什么。 I've gotten this far but the numbers never seem to add up, and I'd like to know how JavaScript comes to its conclusion. 我已经走了这么远,但是数字似乎从来没有加起来,我想知道JavaScript是如何得出结论的。 Maybe I'm missing some fundamental point in it. 也许我在其中缺少一些基本要点。

var nums = new Array(40,20,230,65);

var b = a.sort(function(a,b){

return a-b;

});

so, I work it out like this: 所以,我这样计算:

  1. 40-20 = 20 = (>0) so b,a or 20,40 40-20 = 20 =(> 0)所以b,a或20,40

  2. 230-65 = 165 (>0) so b,a or 230,65 230-65 = 165(> 0),所以b,a或230,65

  3. 40-65 = -25 (<0) so a,b or 40,65 40-65 = -25(<0)所以a,b或40,65

I've run it with those numbers, it does three calculations and comes to 20,40,65,230, which is correct, but I don't know "how"... In what way did it choose numbers to put into the generated sort array. 我已经用这些数字运行了它,它进行了三个计算,得出20、40、65、230,这是正确的,但是我不知道“怎么做”……它是如何选择数字放入生成的排序数组。 I mean, I get six numbers from the answer (the results of the three calculations), so how do I turn those six into the necessary four? 我的意思是,我从答案中得到六个数字(三个计算的结果),那么如何将这六个数字变成必要的四个呢?

Any help. 任何帮助。 I really want to understand this inside out. 我真的很想了解这一点。 :) :)

EDIT: Ok, I'm seeing how this works now, it mucks about with the original array, re-ordering the numbers as per the value returned from the sort function. 编辑:好的,我现在看到它是如何工作的,它与原始数组混为一谈,按照从排序函数返回的值重新排序数字。 But when I use an array of four numbers, I can work it out in two "moves", but JavaScript does three, and the last one seems a bit extraneous. 但是,当我使用四个数字组成的数组时,我可以用两个“动作”来解决这个问题,但是JavaScript可以做到三个,而最后一个似乎有点多余。 Try it and see. 试试看。 :) :)


The last and bestest update!! 最后最好的更新!

Ok, since asking this question I've been mucking about on paper and with JavaScript and I've discovered how the quick sort works-ish. 好的,自从问了这个问题以来,我一直在纸上和JavaScript上做文章,我发现快速排序是如何工作的。 I'm not big on logs but I guess this'll teach me to pay more attention in Math. 我的日志并不大,但是我想这会教会我在数学上给予更多的关注。

I took out my last example to save space, so here's another example of a sort I performed, and how it went. 我拿出最后一个示例来节省空间,所以这是我执行的另一种示例以及它的运行方式。 It matches with the exact same sort I did on paper, except that the order of pairs of numbers JS chooses from the array to sort are somewhat unpredictable, so it actually does more comparisons then I needed to do, but I guess it's because I can think about results ahead of time and choose pairs of numbers as I want (very uncomputer-like). 它与我在纸上进行的排序完全相同,只是JS从数组中选择要排序的数字对的顺序有些不可预测,因此实际上比我需要做的更多比较,但是我想这是因为我可以提前考虑结果,然后根据需要选择数字对(非常类似于计算机)。

In this test I did it in six calculation, JS did it in ten, which, in fairness is more thorough. 在此测试中,我用六次计算完成了此操作,而JS则用十次计算完成了,公平地讲,这更彻底了。

Array == 2,16,7,3,10 数组== 2,16,7,3,10

2 - 16 == -14 (<0) | 2-16 == -14(<0)| 2,16,7,3,10 2,16,7,3,10

16 - 7 == 9 (>0) | 16-7 == 9(> 0)| 2,7,16,3,10 2,7,16,3,10

16 - 3 == 13 (>0) | 16-3 == 13(> 0)| 2,7,3,16,10 2,7,3,16,10

16 - 10 == 6 (>0) | 16-10 == 6(> 0)| 2,7,3,10,16 2,7,3,10,16

2 - 7 == -5 (<0) | 2-7 == -5(<0)| 2,7,3,10,16 2,7,3,10,16

7 - 3 == 4 (>0) | 7-3 == 4(> 0)| 2,3,7,10,16 2,3,7,10,16

Array == 2,3,7,10,16 数组== 2,3,7,10,16

Not sure if this is, strictly speaking, a quick sort, since the quick sort uses a pivot number and uses it as a centre point around which to divide the array in the sort. 严格来讲,不确定这是否是一种快速排序,因为快速排序使用枢轴数并将其用作在数组中围绕数组进行划分的中心点。 But this is my understanding of how JS is doing it. 但这是我对JS如何执行的理解。 Correct me if I'm wrong. 如果我错了纠正我。 :) :)

The implementation uses quick sort algrorithm. 该实现使用快速排序算法。 Read about it here . 在这里阅读。

Given an array of N elements, there are N factorial permutations, of which only one is sorted. 给定N个元素的数组,则有N个阶乘置换,其中仅一个被排序。 Since each comparison divides the permutations into two sets, it should take O(log(N!)) comparisons in order to identify the permutation with the elements are in order, although this depends on the algorithm and original permutation. 由于每个比较将置换分为两组,因此应该进行O(log(N!))比较,以便按元素顺序标识置换,尽管这取决于算法和原始置换。

In the original 4-element array case, you can write a custom sorting routine that makes no assumptions about the original array and guarantees to sort the array in 5 comparisons, but it will get lucky in a third of cases and only use 4 comparisons. 在原始的4元素数组情况下,您可以编写一个自定义排序例程,该例程不对原始数组做任何假设,并保证在5个比较中对数组进行排序,但是在三分之一的情况下它会很幸运,并且仅使用4个比较。 A bubble sort on the other hand would only need 3 comparisons in the unlikely event that the array turned out to be already sorted, but often takes 6 comparisons. 另一方面,冒泡排序仅在极少数情况下只需要进行3次比较,而事实证明该数组已被排序,但通常需要进行6次比较。 Most browsers use one of the standard generic sorting algorithms with well-known performance characteristics. 大多数浏览器使用具有众所周知性能特征的标准通用排序算法之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM