Scripts.FranciscoCharrua.com
Index
News
Downloads
SELECTION-SORT.JS
comparisons:
swaps:
Selection sort involves finding the smallest element in the array and placing it at the beginning. As this is repeated, a sorted partition is created at the beginning of the array. This is done until there are no more unsorted elements left.
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 selection_sort() { this.selection_sort_ascending_order = function(first_input, input, smallest_input) { if(first_input < this.inputs.length - 1) { if(!this.in_ascending_order(smallest_input, input)) { smallest_input = input; } if(input == this.inputs.length - 1) { if(first_input != smallest_input) { setTimeout(this.name + '.selection_sort_swap(' + first_input + ', ' + smallest_input + ');', this.speed); } else { first_input++; setTimeout(this.name + '.selection_sort_ascending_order(' + first_input + ', ' + (first_input + 1) + ', ' + first_input + ');', this.speed); } } else { setTimeout(this.name + '.selection_sort_ascending_order(' + first_input + ', ' + (input + 1) + ', ' + smallest_input + ');', this.speed); } } } this.selection_sort_swap = function(first_input, smallest_input) { this.swap(first_input, smallest_input); first_input++; setTimeout(this.name + '.selection_sort_ascending_order(' + first_input + ', ' + (first_input + 1) + ', ' + first_input + ');', this.speed); } this.selection_sort_ascending_order(0, 1, 0); } var selection; function go() { selection = new sort_array('selection', 10); selection.populate(-100, 100); selection.sort = selection_sort; document.getElementById('selection_populate').onclick = function(){selection.populate(-100, 100);} document.getElementById('selection_reset').onclick = function(){selection.reset();} document.getElementById('selection_sort').onclick = function(){selection.sort();} }