Sorting algorithms/Quicksort: Difference between revisions
(Add Quicksort task) |
(-> IDL) |
||
Line 3: | 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. |
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 |
Revision as of 23:34, 3 October 2007
You are encouraged to solve this task according to the task description, using any language you may know.
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
Heap sort | Merge sort | Patience sort | Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort
In this task, the goal is to sort an array of elements using the 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
IDL has a powerful optimized sort() 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