繁体   English   中英

我想知道Array.prototype.sort是如何工作的,基本上是sort函数的polyfill

[英]I want to know how Array.prototype.sort works basically polyfill of sort function

我知道排序在JS中的工作原理,但想知道Array.prototype.sort是如何实现的,基本上是对sort函数,算法或其他任何细节的polyfill

我在这里找不到https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

如果您有兴趣,请参阅Array.protoype.sort的规范: http : //www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.11

它没有规定要使用哪种排序算法,所以这取决于浏览器的实现。

The time and space complexity of the sort cannot be guaranteed as it is implementation dependent.

 function reverse(a[0..n - 1])
     allocate b[0..n - 1]
     for i from 0 to n - 1
         b[n − 1 − i] := a[i]
     return b

 function reverse_in_place(a[0..n-1])
     for i from 0 to floor((n-2)/2)
         tmp := a[i]
         a[i] := a[n − 1 − i]
         a[n − 1 − i] := tmp

暂无
暂无

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

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