-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (115 loc) · 3 KB
/
script.js
File metadata and controls
132 lines (115 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
let n=40;
let array=[];
let sortingSpeed = 50;
bubbleSortFlag = true;
selectionSortFlag = false;
init();
initFlag= false;
var slider = document.querySelector(".slider");
slider.oninput = function() {
n = this.value;
//console.log(n);
array=[];
init();
}
var slider = document.querySelector(".speedSlider");
slider.oninput = function(){
sortingSpeed = this.value;
}
var bubbleSortBtn = document.querySelector(".bubble_sort");
bubbleSortBtn.addEventListener("click", function() {
selectionSortFlag = false;
bubbleSortFlag = true;
h2 = document.querySelector("h2");
h2.innerHTML = "Bubble Sort"
});
var selectionSortBtn = document.querySelector(".selection_sort");
selectionSortBtn.addEventListener("click", function() {
bubbleSortFlag = false;
selectionSortFlag = true;
h2 = document.querySelector("h2");
h2.innerHTML = "Selection Sort"
});
function init(){
initFlag = true;
for(let i=0;i<n;i++){
array[i]=Math.random();
}
showBars();
}
function play(){
initFlag = false;
let moves= [];
if (selectionSortFlag == true ){
console.log("executing selection sort")
moves=selectionSort([...array]);
}
else if (bubbleSortFlag ==true) {
console.log("executing bubble sort")
moves=bubbleSort([...array]);
}
animate(moves);
}
function animate(moves){
if(initFlag==true){
moves = [];
}
if(moves.length==0){
showBars();
return;
}
const move=moves.shift(0);
const [i,j] = move.indices;
if (move.type == "swap"){
[array[i],array[j]]=[array[j],array[i]];
}
showBars(move);
setTimeout(function(){
animate(moves);
},sortingSpeed);
}
function selectionSort(arr) {
const moves=[];
for (let i = 0; i < arr.length; i++) {
let lowest = i
for (let j = i + 1; j < arr.length; j++) {
moves.push({indices: [j,lowest], type : "comp"});
if (arr[j] < arr[lowest]) {
lowest = j
}
}
if (lowest !== i) {
// Swap
moves.push({indices: [i,lowest], type : "swap"});
;[arr[i], arr[lowest]] = [arr[lowest], arr[i]]
}
}
return moves;
}
function bubbleSort(array){
const moves=[];
do{
var swapped=false;
for(let i=1;i<array.length;i++){
moves.push({indices: [i-1,i], type : "comp"});
if(array[i-1]>array[i]){
moves.push({indices: [i-1,i], type : "swap"});
swapped=true;
[array[i-1],array[i]]=[array[i],array[i-1]];
}
}
}while(swapped);
return moves;
}
function showBars(move){
container.innerHTML="";
for(let i=0;i<array.length;i++){
const bar=document.createElement("div");
bar.style.height=array[i]*100+"%";
bar.classList.add("bar");
if(move && move.indices.includes(i)){
bar.style.backgroundColor=move.type=="swap"?"red":"blue";
}
container.appendChild(bar);
}
}