-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
381 lines (341 loc) · 14.3 KB
/
script.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// 配列を視覚化する関数
function visualizeArray(array) {
const container = document.getElementById('array-container');
container.innerHTML = '';
const maxValue = Math.max(...array);
array.forEach((value) => {
const bar = document.createElement('div');
bar.className = 'array-bar';
const width = (value / maxValue) * 100;
bar.style.width = `${width}%`;
const valueSpan = document.createElement('span');
valueSpan.className = 'array-bar-value';
valueSpan.textContent = value;
bar.appendChild(valueSpan);
container.appendChild(bar);
});
}
document.addEventListener('DOMContentLoaded', () => {
const algorithmSelect = document.getElementById('algorithm-select');
const runBtn = document.getElementById('run-btn');
const stepBtn = document.getElementById('step-btn');
const speedControl = document.getElementById('speed-control');
const speedValue = document.getElementById('speed-value');
const randomDataBtn = document.getElementById('random-data');
const manualInput = document.getElementById('manual-input');
const explanationArea = document.getElementById('algorithm-description');
const searchBtn = document.getElementById('search-btn');
const searchTargetInput = document.getElementById('search-target');
const searchResultText = document.getElementById('search-result-text');
let currentAlgorithm = null;
let data = [];
let animationSpeed = 1000; // ミリ秒単位のアニメーション速度
let searchTarget = null;
// アルゴリズム選択時の処理
algorithmSelect.addEventListener('change', () => {
currentAlgorithm = algorithmSelect.value;
updateExplanation(currentAlgorithm);
});
// 実行ボタンクリック時の処理
runBtn.addEventListener('click', async () => {
if (currentAlgorithm && data.length > 0) {
console.log(`${currentAlgorithm}を実行します。データ: ${data}`);
switch (currentAlgorithm) {
case 'selection':
await selectionSort([...data]);
break;
case 'bubble':
await bubbleSort([...data]);
break;
case 'insertion':
await insertionSort([...data]);
break;
case 'quick':
await quickSort([...data]);
break;
case 'merge':
await mergeSort([...data]);
break;
default:
alert('選択されたアルゴリズムはまだ実装されていません。');
}
} else {
alert('アルゴリズムを選択し、データを入力してください。');
}
});
// ステップ実行ボタンクリック時の処理
stepBtn.addEventListener('click', () => {
if (currentAlgorithm && data.length > 0) {
console.log(`${currentAlgorithm}をステップ実行します。`);
// ここにステップ実行のコードを追加
} else {
alert('アルゴリズムを選択し、データを入力してください。');
}
});
// 速度調整時の処理
speedControl.addEventListener('input', () => {
speedValue.textContent = `x${speedControl.value}`;
animationSpeed = 1000 / parseFloat(speedControl.value);
});
// ランダムデータ生成ボタンクリック時の処理
randomDataBtn.addEventListener('click', () => {
data = generateRandomData(10);
manualInput.value = data.join(', ');
updateDataVisualization();
});
// 手動入力時の処理
manualInput.addEventListener('change', () => {
data = manualInput.value.split(',').map(num => parseInt(num.trim())).filter(num => !isNaN(num));
updateDataVisualization();
});
// ランダムデータ生成関数
function generateRandomData(length) {
return Array.from({length}, () => Math.floor(Math.random() * 100));
}
// アルゴリズムの説明を更新する関数
function updateExplanation(algorithm) {
const explanations = {
'bubble': 'バブルソート: 隣接する要素を比較し、必要に応じて交換を繰り返すシンプルなソートアルゴリズムです。',
'selection': '選択ソート: 未ソートの部分から最小値を選択し、ソート済み部分の末尾に追加していくアルゴリズムです。',
'insertion': '挿入ソート: ソート済み部分に新しい要素を適切な位置に挿入していくアルゴリズムです。',
'quick': 'クイックソート: ピボットを選んで分割統治法を用いる効率的なソートアルゴリズムです。',
'merge': 'マージソート: 分割統治法を用いて配列を分割し、ソートしながらマージするアルゴリズムです。',
'linear': '線形探索: 配列の先頭から順に探索する単純なアルゴリズムです。',
'binary': '二分探索: ソート済みの配列で、中央の値と比較しながら探索範囲を半分に絞っていくアルゴリズムです。',
'dfs': '深さ優先探索(DFS): グラフやツリーを探索する際に、可能な限り深く進んでから戻るアルゴリズムです。',
'bfs': '幅優先探索(BFS): グラフやツリーを探索する際に、現在の深さのノードを全て探索してから次の深さに進むアルゴリズムです。'
};
explanationArea.textContent = explanations[algorithm] || 'アルゴリズムを選択してください。';
}
// データが変更されたときに配列を視覚化
function updateDataVisualization() {
visualizeArray(data);
}
// 探索ボタンのイベントリスナー
searchBtn.addEventListener('click', async () => {
if (currentAlgorithm && data.length > 0) {
searchTarget = parseInt(searchTargetInput.value);
if (isNaN(searchTarget)) {
alert('有効な探索対象を入力してください。');
return;
}
console.log(`${currentAlgorithm}を実行します。探索対象: ${searchTarget}`);
let result;
switch (currentAlgorithm) {
case 'linear':
result = await linearSearch([...data], searchTarget);
break;
case 'binary':
result = await binarySearch([...data].sort((a, b) => a - b), searchTarget);
break;
case 'dfs':
case 'bfs':
alert('グラフ探索アルゴリズムはこの例では実装していません。');
return;
default:
alert('選択された探索アルゴリズムはまだ実装されていません。');
return;
}
displaySearchResult(result);
} else {
alert('アルゴリズムを選択し、データを入力してください。');
}
});
function displaySearchResult(result) {
const searchResultText = document.getElementById('search-result-text');
if (result !== -1) {
searchResultText.textContent = `探索対象 ${searchTarget} が位置 ${result} で見つかりました。`;
} else {
searchResultText.textContent = `探索対象 ${searchTarget} は見つかりませんでした。`;
}
}
// 選択ソートアルゴリズムの実装
async function selectionSort(arr) {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
let minIdx = i;
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
if (minIdx !== i) {
// 要素の交換
[arr[i], arr[minIdx]] = [arr[minIdx], arr[i]];
visualizeArray(arr);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
}
}
return arr;
}
// バブルソートの実装
async function bubbleSort(arr) {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
visualizeArray(arr);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
}
}
}
return arr;
}
// クイックソートの実装
async function quickSort(arr, low = 0, high = arr.length - 1) {
if (low < high) {
const pi = await partition(arr, low, high);
await quickSort(arr, low, pi - 1);
await quickSort(arr, pi + 1, high);
}
return arr;
}
async function partition(arr, low, high) {
const pivot = arr[high];
let i = low - 1;
for (let j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
visualizeArray(arr);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
}
}
[arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
visualizeArray(arr);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
return i + 1;
}
// 挿入ソートの実装
async function insertionSort(arr) {
const n = arr.length;
for (let i = 1; i < n; i++) {
let key = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
visualizeArray(arr);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
}
arr[j + 1] = key;
visualizeArray(arr);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
}
return arr;
}
// マージソートの実装
async function mergeSort(arr, left = 0, right = arr.length - 1) {
if (left < right) {
const mid = Math.floor((left + right) / 2);
await mergeSort(arr, left, mid);
await mergeSort(arr, mid + 1, right);
await merge(arr, left, mid, right);
}
return arr;
}
async function merge(arr, left, mid, right) {
const n1 = mid - left + 1;
const n2 = right - mid;
const L = new Array(n1);
const R = new Array(n2);
for (let i = 0; i < n1; i++) L[i] = arr[left + i];
for (let j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];
let i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
visualizeArray(arr);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
visualizeArray(arr);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
visualizeArray(arr);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
}
}
async function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
visualizeSearchArray(arr, i);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
if (arr[i] === target) {
return i;
}
}
return -1;
}
async function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
visualizeSearchArray(arr, mid, left, right);
await new Promise(resolve => setTimeout(resolve, animationSpeed));
if (arr[mid] === target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
function visualizeSearchArray(arr, current, left = 0, right = arr.length - 1) {
const container = document.getElementById('search-container');
container.innerHTML = '';
const maxHeight = 150; // 最大の高さをさらに小さく
const maxValue = Math.max(...arr);
arr.forEach((value, index) => {
const element = document.createElement('div');
element.className = 'search-element';
const height = Math.max((value / maxValue) * maxHeight, 5); // 最小高さを5pxに
element.style.height = `${height}px`;
element.setAttribute('data-value', value); // 値をdata属性として設定
if (index === current) {
element.style.backgroundColor = '#e74c3c';
} else if (index >= left && index <= right) {
element.style.backgroundColor = '#2ecc71';
}
container.appendChild(element);
});
}
function visualizeArray(array) {
const container = document.getElementById('array-container');
container.innerHTML = '';
const maxValue = Math.max(...array);
array.forEach((value, index) => {
const bar = document.createElement('div');
bar.className = 'array-bar';
const width = (value / maxValue) * 100;
bar.style.width = `${width}%`;
const valueSpan = document.createElement('span');
valueSpan.className = 'array-bar-value';
valueSpan.textContent = value;
bar.appendChild(valueSpan);
container.appendChild(bar);
});
}
function displayResult(message) {
const resultElement = document.getElementById('result');
resultElement.textContent = message;
}
// 初期表示
updateDataVisualization();
});