Sorting algorithms/Quicksort: Difference between revisions

-> IDL
(Add Quicksort task)
 
(-> IDL)
Line 3:
 
In this task, the goal is to sort an array of elements using the [http://en.wikipedia.org/wiki/Quicksort Quicksort] algorithm. The elements must have a total order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
 
==[[IDL]]==
[[Category:IDL]]
 
IDL has a powerful optimized <tt>sort()</tt> built-in. The following is thus merely for demonstration.
 
function qs, arr
if (count = n_elements(arr)) lt 2 then return,arr
pivot = total(arr) / count ; use the average for want of a better choice
return,[qs(arr[where(arr le pivot)]),qs(arr[where(arr gt pivot)])]
end
 
Example:
 
IDL> print,qs([3,17,-5,12,99])
-5 3 12 17 99
Anonymous user