Bubblesort ukázka

<html>

<script> function test() {

var x = [1, 5, 6, 7, 8];

bubbleSort(x);

}

function bubbleSort(array) {

var step = 0;
for (var i = 0; i < array.length - 1; i++) {
for (var j = 0; j < array.length - 1 - i; j++) {
printStep(array, step++);
if (array[j] < array[j + 1]) {
var tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}

}

function printStep(array, step) {

var container = document.getElementById("container");
container.innerHTML += "Krok " + step + ": "
for (var index = 0; index < array.length; index++) {
container.innerHTML += array[index] + " ";
}
container.innerHTML += "<br />";

}

</script>

<div id="container">
</div>
<script src="bubblesort.js"></script>

<button onclick="test()">Test me!</button>

</html>