40 lines
959 B
Plaintext
40 lines
959 B
Plaintext
== Quicksort ==
|
|
Stats::
|
|
:: Worst Case Performance: O(n^2^)
|
|
:: Best Case Performance: O(n log n)
|
|
|
|
=== Algorithm ===
|
|
# Pick an element, called a *pivot*, from the list.
|
|
# Make three lists:
|
|
* before: all elements smaller than pivot
|
|
* pivot: all elements equal to pivot
|
|
* after: all elements larger than pivot
|
|
# recurse on before-list and after-list, then return concat(before, pivot, after)
|
|
|
|
=== Psuedocode ===
|
|
{{{
|
|
function quicksort(items) {
|
|
if(count(items) <= 1) {
|
|
return a
|
|
}
|
|
middle_idx = count(items)/2;
|
|
pivot = [];
|
|
pivot[] = items[middle_idx];
|
|
before_list = after_list = [];
|
|
for(i in items) {
|
|
if(items[i] < pivot) {
|
|
before_list[] = items[i];
|
|
} else if(items[i] > pivot) {
|
|
after_list[] = items[i];
|
|
} else {
|
|
pivot[] = items[i];
|
|
}
|
|
}
|
|
return concatenate(quicksort(before_list), pivot, quicksort(after_list));
|
|
}
|
|
}}}
|
|
|
|
=== Additional Info ===
|
|
Wikipedia Article [http://en.wikipedia.org/wiki/Quicksort]
|
|
|