-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathessentials.js
More file actions
180 lines (153 loc) · 4.67 KB
/
essentials.js
File metadata and controls
180 lines (153 loc) · 4.67 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// DECLARING ESSENTIALS
const algoList = [
["Bubble Sort", "bubble"],
["Insertion Sort", "insertion"],
["Selection Sort", "selection"]
];
const firstAlgo = "bubble";
/* ******************************************************************* */
// QUERYING DOM ELEMENTS
const inputSizeRange = document.getElementById("inputSize");
const sortAlgo = document.getElementById("sortAlgo");
const startBtn = document.getElementById("start");
const generateBtn = document.getElementById("generate");
const arrayNode = document.querySelector(".array");
const previewCode = document.getElementById("code");
/* ******************************************************************* */
//ESSENTIAL FUNCTIONS
const enableArrayInputs = () => {
inputSizeRange.title = "";
generateBtn.title = "";
startBtn.title = "";
sortAlgo.title = "";
inputSizeRange.disabled = false;
generateBtn.disabled = false;
startBtn.disabled = false;
sortAlgo.disabled = false;
};
const disableArrayInputs = () => {
inputSizeRange.title = "Wait till the simulation finishes!";
generateBtn.title = "Wait till the simulation finishes!";
startBtn.title = "Wait till the simulation finishes!";
sortAlgo.title = "Wait till the simulation finishes!";
inputSizeRange.disabled = true;
generateBtn.disabled = true;
startBtn.disabled = true;
sortAlgo.disabled = true;
};
const sleep = (duration) => {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, duration * 1000)
})
};
const setClass = (cellNode, className=null) => {
if(!className)
cellNode.className = "cell";
else
cellNode.className = "cell "+className;
};
const generateRandom = (min, max) => Math.floor(Math.random() * (max - min) + min);
const createCell = () =>{
let cellNode = document.createElement("div");
cellNode.textContent = generateRandom(5, 1000);
setClass(cellNode);
return cellNode;
}
const renderArray = () => {
arrayNode.innerHTML = "";
let arrayLength = parseInt(inputSizeRange.value);
for(let i = 0; i < arrayLength; i++)
arrayNode.appendChild(createCell());
};
const changeSize = () => {
let cellArray = document.querySelectorAll(".cell");
let curValue = cellArray.length;
let newValue = parseInt(inputSizeRange.value);
let change = newValue - curValue;
if(change > 0){
for(let i = 0; i < change; i++)
arrayNode.appendChild(createCell());
}
else{
for(let i = 0; i < Math.abs(change); i++)
arrayNode.removeChild(cellArray[curValue - 1 - i]);
}
};
const loadAlgoList = () => {
for(let i = 0; i < algoList.length; i++){
optionEl = document.createElement("option");
optionEl.textContent = algoList[i][0]
optionEl.value = algoList[i][1];
sortAlgo.appendChild(optionEl);
}
document.querySelector(`option[value="${firstAlgo}"]`).selected = true;
}
const changeCode = () => {
let algo = sortAlgo.value;
switch(algo){
case "bubble":{
previewCode.textContent = `
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap arr[j+1] and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
`;
break;
}
case "insertion":{
previewCode.textContent = `
void insertionSort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
`;
break;
}
case "selection":{
previewCode.textContent = `
void selectionSort(int arr[])
{
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
`;
break;
}
}
};