Skip to content

Commit 327b9ce

Browse files
committed
Bubble Sort Code Push
1 parent ee3ccfa commit 327b9ce

File tree

4 files changed

+57
-58
lines changed

4 files changed

+57
-58
lines changed

animations/moveAnimation.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function animate(moves){
2+
if(moves.length==0){return;}
3+
4+
const move=moves.shift(); //i and j contains i-1 and i.
5+
const [i,j] = move.indices;
6+
if(move.type == "swap"){
7+
[array[i],array[j]] = [array[j],array[i]];
8+
}
9+
10+
playNote(200+array[i]*500); //linear interpolation function.
11+
playNote(200+array[j]*500);
12+
showBars(move);
13+
setTimeout(function(){
14+
animate(moves);
15+
},50);
16+
};

animations/playNote.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let audioCtx = null;
2+
3+
function playNote(freq){
4+
if(audioCtx==null){
5+
audioCtx= new(
6+
AudioContext ||
7+
webkitAudioContext ||
8+
window.webkitAudioContext
9+
)();
10+
}
11+
12+
const dur = 0.1;
13+
const osc = audioCtx.createOscillator();
14+
osc.frequency.value = freq;
15+
osc.start();
16+
osc.stop(audioCtx.currentTime+dur);
17+
const node = audioCtx.createGain();
18+
node.gain.value=0.1;
19+
node.gain.linearRampToValueAtTime(
20+
0,audioCtx.currentTime+dur
21+
);
22+
osc.connect(node);
23+
node.connect(audioCtx.destination);
24+
}

index.html

+3
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ <h3>Easy to visualize and understand the sorting Algorithms with Sound Effects!<
2222

2323
<script src="script.js"></script>
2424
<script src="sorting_algo/bubbleSort.js"></script>
25+
<script src="animations/playNote.js"></script>
26+
<script src="animations/moveAnimation.js"></script>
27+
<script src="animations/showBars.js"></script>
2528
</body>
2629
</html>

script.js

+14-58
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,7 @@
1-
const n=25;
1+
const n=50;
22
const array = [];
33
init();
44

5-
let audioCtx = null;
6-
7-
function playNote(freq){
8-
if(audioCtx==null){
9-
audioCtx= new(
10-
AudioContext ||
11-
webkitAudioContext ||
12-
window.webkitAudioContext
13-
)();
14-
}
15-
16-
const dur = 0.1;
17-
const osc = audioCtx.createOscillator();
18-
osc.frequency.value = freq;
19-
osc.start();
20-
osc.stop(audioCtx.currentTime+dur);
21-
const node = audioCtx.createGain();
22-
node.gain.value=0.1;
23-
node.gain.linearRampToValueAtTime(
24-
0,audioCtx.currentTime+dur
25-
);
26-
osc.connect(node);
27-
node.connect(audioCtx.destination);
28-
}
29-
30-
function init(){
31-
for(i=0;i<n;i++){
32-
array[i] = Math.random();
33-
}
34-
showBars();
35-
}
36-
37-
function play(){
38-
const copyArray = [...array];
39-
const moves = bubbleSort(copyArray);
40-
animate(moves);
41-
}
42-
43-
function animate(moves){
44-
if(moves.length==0){return;}
45-
46-
const move=moves.shift(); //i and j contains i-1 and i.
47-
const [i,j] = move.indices;
48-
if(move.type == "swap"){
49-
[array[i],array[j]] = [array[j],array[i]];
50-
}
51-
52-
playNote(200+array[i]*500); //linear interpolation function.
53-
playNote(200+array[j]*500);
54-
showBars(move);
55-
setTimeout(function(){
56-
animate(moves);
57-
},50);
58-
}
59-
60-
61-
625
function showBars(move){
636

647
container.innerHTML="";
@@ -75,4 +18,17 @@ function showBars(move){
7518
}
7619
container.appendChild(bar);
7720
}
21+
};
22+
23+
function init(){
24+
for(i=0;i<n;i++){
25+
array[i] = Math.random();
26+
}
27+
showBars();
28+
}
29+
30+
function play(){
31+
const copyArray = [...array];
32+
const moves = bubbleSort(copyArray);
33+
animate(moves);
7834
}

0 commit comments

Comments
 (0)