53

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?

1
  • The stability of Ruby's sort is addressed in this question. Commented Jun 11, 2017 at 16:06

1 Answer 1

35

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.

Sign up to request clarification or add additional context in comments.

7 Comments

This also means it is most likely not a stable sort. See the explanations of this at en.wikipedia.org/wiki/Quick_sort
If the array is almost sorted, only a stupid quicksort goes to n^2. Median of three pivot selection is in all implementattions I've used
@Stephan Eggermont: As far as I know, also median of three pivot is n^2 in the worse case.
I'm wondering if the word "stupid" is actually a technical word for describing these algorithms
@CarlosMartinez The technical term for "stupid" is "naive".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.