71 lines
1.5 KiB
HTML
71 lines
1.5 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<head>
|
|
<link rel="Stylesheet" type="text/css" href="style.css">
|
|
<title>Quicksort</title>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
</head>
|
|
<body>
|
|
|
|
<h2 id="toc_0.1">Quicksort</h2>
|
|
<dl>
|
|
<dt>Stats</dt>
|
|
<dd>Worst Case Performance: O(n<sup><small>2</small></sup>)</dd>
|
|
<dd>Best Case Performance: O(n log n)</dd>
|
|
</dl>
|
|
|
|
<h3 id="toc_0.1.1">Algorithm</h3>
|
|
<ol>
|
|
<li>
|
|
Pick an element, called a <strong>pivot</strong>, from the list.
|
|
|
|
<li>
|
|
Make three lists:
|
|
|
|
<ul>
|
|
<li>
|
|
before: all elements smaller than pivot
|
|
|
|
<li>
|
|
pivot: all elements equal to pivot
|
|
|
|
<li>
|
|
after: all elements larger than pivot
|
|
|
|
</ul>
|
|
<li>
|
|
recurse on before-list and after-list, then return concat(before, pivot, after)
|
|
|
|
</ol>
|
|
|
|
<h3 id="toc_0.1.2">Psuedocode</h3>
|
|
<pre>
|
|
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));
|
|
}
|
|
</pre>
|
|
|
|
<h3 id="toc_0.1.3">Additional Info</h3>
|
|
<p>
|
|
Wikipedia Article [<a href="http://en.wikipedia.org/wiki/Quicksort]">http://en.wikipedia.org/wiki/Quicksort]</a>
|
|
</p>
|
|
|
|
</body>
|
|
</html>
|