diff options
Diffstat (limited to 'layouts')
-rw-r--r-- | layouts/shortcodes/sort.html | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/layouts/shortcodes/sort.html b/layouts/shortcodes/sort.html new file mode 100644 index 0000000..a748bd1 --- /dev/null +++ b/layouts/shortcodes/sort.html @@ -0,0 +1,36 @@ +<button onclick="run()">Run visualization</button> +<pre id="visualization"> +</pre> +<script> +let running = false + +function render(n, A, i, j) { + let line1 = Array(n).fill(" ") + line1[j] = "j" + line1[i] = "i" + document.getElementById("visualization").innerHTML = line1.join(" ") + "\n" + A.join(" ") +} + +async function run() { + if (running) return + running = true + + let n = 10 + let A = Array(n).fill().map(() => Math.floor(Math.random() * 10)) + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + render(n, A, i, j) + await new Promise(resolve => setTimeout(resolve, 500)) + if (A[i] < A[j]) { + let tmp = A[i] + A[i] = A[j] + A[j] = tmp + render(n, A, i, j) + await new Promise(resolve => setTimeout(resolve, 500)) + } + } + } + + running = false +} +</script> |