Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which algorithm does Ruby's sort method use?

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

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

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

like image 229
unj2 Avatar asked May 13 '09 02:05

unj2


People also ask

What algorithm is used in collections sort?

So, in the end, Collections#sort uses Arrays#sort (of object elements) behind the scenes. This implementation uses merge sort or tim sort. Show activity on this post. According to the Javadoc, only primitive arrays are sorted using Quicksort.

Which algorithm is used in sorted Python?

Timsort has been Python's standard sorting algorithm since version 2.3. It is also used to sort arrays of non-primitive type in Java SE 7, on the Android platform, in GNU Octave, on V8, Swift, and Rust.

Which algorithm is best for sorted array?

​Many sorting algorithms are available, but the one which is best suited for the almost sorted array is the insertion sort.

What sort algorithm does swift use?

IntroSort. IntroSort is the algorithm used by swift to sort a collection. Introsort is an hybrid algorithm invented by David Musser in 1993 with the purpose of giving a generic sorting algorithm for the C++ standard library.


1 Answers

Look here: 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.

like image 152
AlbertoPL Avatar answered Oct 01 '22 10:10

AlbertoPL