Ruby的排序方法使用哪种算法?

时间:2021-04-07 16:21:01

When I sort an Array using the native sort method, which algorithm does Ruby use?

当我使用本机排序方法对数组进行排序时,Ruby使用哪种算法?

Is it data-dependant, i.e., if the data is small it uses X algorithm else it uses Y algorithm?

data-dependant,即。,如果数据很小,就用X算法,还是用Y算法?

Is it a stable sort? What is the average time complexity?

它是稳定的吗?平均时间复杂度是多少?

1 个解决方案

#1


26  

Look here: http://www.igvita.com/2009/03/26/ruby-algorithms-sorting-trie-heaps/

看这里:http://www.igvita.com/2009/03/26/ruby-algorithms-sorting-trie-heaps/

It does natively use quicksort however, which is n log n complexity on average.

但是它实际使用的是快速排序,但是平均复杂度是n log n。

#1


26  

Look here: http://www.igvita.com/2009/03/26/ruby-algorithms-sorting-trie-heaps/

看这里:http://www.igvita.com/2009/03/26/ruby-algorithms-sorting-trie-heaps/

It does natively use quicksort however, which is n log n complexity on average.

但是它实际使用的是快速排序,但是平均复杂度是n log n。