Last active
October 12, 2021 00:48
-
-
Save Kazuki-Naruse/8f0213e5ccc2fa7df4aa39af80be7659 to your computer and use it in GitHub Desktop.
SortAlgorithm2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SortAlgorithm2 | |
const { Tracer, Array1DTracer, Randomize, Layout, VerticalLayout } = require('algorithm-visualizer'); | |
const tracer = new Array1DTracer(); | |
Layout.setRoot(new VerticalLayout([tracer])); | |
const Arr = Randomize.Array1D({ N: 10 }); | |
tracer.set(Arr); | |
Tracer.delay(); | |
function sort(Arr, low, high) { | |
for(let i = low; i <= high; i++){ | |
tracer.select(i); | |
Tracer.delay(); | |
for(let j = i + 1; j <= high; j++){ | |
tracer.select(j); | |
Tracer.delay(); | |
if(Arr[i] > Arr[j]){ | |
tracer.patch(i, Arr[j]); | |
tracer.patch(j, Arr[i]); | |
[Arr[i], Arr[j]] = [Arr[j], Arr[i]]; | |
Tracer.delay(); | |
tracer.depatch(i); | |
tracer.depatch(j); | |
} | |
if(i === j) continue; | |
tracer.deselect(j); | |
} | |
tracer.deselect(i); | |
} | |
} | |
function sortAlgorithm2(Arr) { | |
sort(Arr, 0, Arr.length - 1); | |
} | |
sortAlgorithm2(Arr); | |
// https://gist.github.com/Kazuki-Naruse/7f631f2fc5456b61ae194966f9a90df9 | |
// ↑改良版 | |
// https://algorithm-visualizer.org/ | |
// こちらのサイトにコピペして、Build、Playすると確認できます。 | |
// 青色が比較対象データで、ピンク色になったときに双方を入れ替えてます。 | |
// https://arxiv.org/pdf/2110.01111.pdf | |
// アルゴリズムの詳細 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment