Scripts.FranciscoCharrua.com
Index
News
Downloads
QUICK-SORT.JS
comparisons:
swaps:
Quicksort works by partitioning an array around a chosen pivot point. Each partition is then treated the same way. This is done recursively until the whole array is sorted.
sort.js
bubble-sort.js
optimized-bubble-sort.js
selection-sort.js
cocktail-sort.js
optimized-cocktail-sort.js
odd-even-sort.js
comb-sort.js
quick-sort.js
gnome-sort.js
optimized-gnome-sort.js
compare sort algorithms
function quick_sort() { this.partition = function(left, pivot, right) { this.left = left; this.pivot = pivot; this.right = right; } this.quick_sort = function(input) { if(input == this.partitions[this.current_partition].pivot) { if(this.partitions[this.current_partition].left + 1 < this.partitions[this.current_partition].pivot) { this.partitions[this.partitions.length] = new this.partition(this.partitions[this.current_partition].left, this.partitions[this.current_partition].pivot - 1, this.partitions[this.current_partition].pivot - 1); } if(this.partitions[this.current_partition].pivot + 1 < this.partitions[this.current_partition].right) { this.partitions[this.partitions.length] = new this.partition(this.partitions[this.current_partition].pivot + 1, this.partitions[this.current_partition].right, this.partitions[this.current_partition].right); } if(this.current_partition < this.partitions.length - 1) { this.current_partition++; setTimeout(this.name + '.quick_sort(' + this.partitions[this.current_partition].left + ');', this.speed); } } else { if(this.in_ascending_order(input, this.partitions[this.current_partition].pivot)) { setTimeout(this.name + '.quick_sort(' + (input + 1) + ');', this.speed); } else { setTimeout(this.name + '.quick_sort_swap(' + input + ');', this.speed); } } } this.quick_sort_swap = function(input) { if(input + 1 == this.partitions[this.current_partition].pivot) { this.swap(input, this.partitions[this.current_partition].pivot); this.partitions[this.current_partition].pivot = input; this.quick_sort(this.partitions[this.current_partition].pivot); } else { this.swap(input, this.partitions[this.current_partition].pivot - 1); setTimeout(this.name + '.quik_sort_advance_pivot(' + input + ');', this.speed); } } this.quik_sort_advance_pivot = function(input) { this.swap(this.partitions[this.current_partition].pivot - 1, this.partitions[this.current_partition].pivot) this.partitions[this.current_partition].pivot--; setTimeout(this.name + '.quick_sort(' + input + ');', this.speed); } this.partitions = Array(); this.partitions[0] = new this.partition(0, this.inputs.length - 1, this.inputs.length - 1); this.current_partition = 0; this.quick_sort(0); } var quick; function go() { quick = new sort_array('quick', 10); quick.populate(-100, 100); quick.sort = quick_sort; document.getElementById('quick_populate').onclick = function(){quick.populate(-100, 100);} document.getElementById('quick_reset').onclick = function(){quick.reset();} document.getElementById('quick_sort').onclick = function(){quick.sort();} }