Skip to content

Instantly share code, notes, and snippets.

@Kazuki-Naruse
Last active October 19, 2021 06:35
Show Gist options
  • Save Kazuki-Naruse/b7be617ba6add63aeb36563af4d433d3 to your computer and use it in GitHub Desktop.
Save Kazuki-Naruse/b7be617ba6add63aeb36563af4d433d3 to your computer and use it in GitHub Desktop.
SortAlgorithm1
// SortAlgorithm1
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 = low; 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 sortAlgorithm1(Arr) {
sort(Arr, 0, Arr.length - 1);
}
sortAlgorithm1(Arr);
// https://gist.github.com/Kazuki-Naruse/8f0213e5ccc2fa7df4aa39af80be7659\
// ↑改良版
// 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