开发者

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. 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. 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

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

  3. 40-65 = -25 (<0) so a,b or 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. 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. 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. 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 we开发者_Go百科nt. 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).

In this test I did it in six calculation, JS did it in ten, which, in fairness is more thorough.

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

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

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

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

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

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

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

Array == 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. 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. 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.

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. 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. Most browsers use one of the standard generic sorting algorithms with well-known performance characteristics.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜