Quicksort

Sort an array using the quicksort algorithm with average O(n log n) time complexity.

Code

Algorithms
def quicksort(a)
  return a if a.length <= 1
  pivot = a[0]
  quicksort(a[1..].select { |x| x <= pivot }) + [pivot] + quicksort(a[1..].select { |x| x > pivot })
end
quicksort(arr)

Parameters

Array to sort

Server

More Ruby Snippets