-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
259 lines (252 loc) · 8.52 KB
/
index.html
File metadata and controls
259 lines (252 loc) · 8.52 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sorting Visualization</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
transition: background-color 0.3s, color 0.3s;
}
body {
text-align: center;
background-color: #f4f4f4;
font-family: Arial, sans-serif;
color: #333;
}
.dark-mode {
background-color: #121212;
color: #fff;
}
.header {
padding: 20px;
background-color: #fd0081;
color: white;
font-size: 1.5em;
font-weight: bold;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);
display: flex;
justify-content: center;
align-items: center;
position: relative;
text-align: center;
}
.dark-mode .header {
background-color: #00bcd4;
}
.toggle-container {
position: absolute;
right: 20px;
}
.controls {
margin: 20px;
}
.bar-container {
display: flex;
justify-content: center;
align-items: flex-end;
height: 400px;
margin: 20px auto;
width: 80%;
border: 1px solid black;
background: #fff;
padding: 10px;
border-radius: 10px;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);
}
.dark-mode .bar-container {
background: #222;
border-color: #444;
}
.bar {
width: 10px;
background-color: #fd0081;
margin: 1px;
border-radius: 5px;
}
.dark-mode .bar {
background-color: #00bcd4;
}
button {
margin: 5px;
padding: 10px;
cursor: pointer;
background-color: white;
border: none;
border-radius: 5px;
color: #fd0081;
font-weight: bold;
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
}
button:hover {
background-color: #fd0081;
color: white;
}
.dark-mode button {
background-color: #444;
color: white;
}
.dark-mode button:hover {
background-color: #00bcd4;
}
input[type="range"] {
accent-color: #fd0081;
}
.dark-mode input[type="range"] {
accent-color: #00bcd4;
}
@media (max-width: 600px) {
.header {
flex-direction: column;
text-align: center;
}
.toggle-container {
position: static;
margin-top: 10px;
}
}
</style>
</head>
<body>
<div class="header">
<h1>Sorting Visualizer</h1>
<div class="toggle-container">
<button onclick="toggleDarkMode()">Dark Mode</button>
</div>
</div>
<div class="controls">
<input type="color" id="barColor" name="barColor" value="#fd0081" onchange="changeBarColor()">
<label for="speed">Animation Speed:</label>
<input type="range" id="speed" name="speed" min="10" max="1000" value="100" oninput="updateSpeed()">
<button onclick="generateArray()">Generate New Array</button>
<button onclick="bubbleSort()">Bubble Sort</button>
<button onclick="selectionSort()">Selection Sort</button>
<button onclick="insertionSort()">Insertion Sort</button>
<button onclick="mergeSort()">Merge Sort</button>
<button onclick="quickSort()">Quick Sort</button>
</div>
<div class="bar-container" id="bar-container"></div>
<p id="swap-count">Swaps: 0</p>
<script>
let bars = [];
let swapCount = 0;
let speed = 100;
function updateSpeed() {
speed = document.getElementById("speed").value;
}
function generateArray() {
const container = document.getElementById("bar-container");
container.innerHTML = "";
bars = [];
swapCount = 0;
updateSwapCount();
for (let i = 0; i < 50; i++) {
let value = Math.floor(Math.random() * 350) + 10;
let bar = document.createElement("div");
bar.style.height = `${value}px`;
bar.classList.add("bar");
bars.push(bar);
container.appendChild(bar);
}
}
function changeBarColor() {
let color = document.getElementById("barColor").value;
document.querySelectorAll(".bar").forEach(bar => {
bar.style.backgroundColor = color;
});
}
function updateSwapCount() {
document.getElementById("swap-count").innerText = `Swaps: ${swapCount}`;
}
async function swap(i, j) {
let temp = bars[i].style.height;
bars[i].style.height = bars[j].style.height;
bars[j].style.height = temp;
swapCount++;
updateSwapCount();
await new Promise(resolve => setTimeout(resolve, 50));
}
async function bubbleSort() {
swapCount = 0;
updateSwapCount();
for (let i = 0; i < bars.length; i++) {
for (let j = 0; j < bars.length - i - 1; j++) {
if (bars[j].offsetHeight > bars[j + 1].offsetHeight) {
await swap(j, j + 1);
}
}
}
}
async function selectionSort() {
for (let i = 0; i < bars.length; i++) {
let minIndex = i;
for (let j = i + 1; j < bars.length; j++) {
if (bars[j].offsetHeight < bars[minIndex].offsetHeight) {
minIndex = j;
}
}
await swap(i, minIndex);
}
}
async function insertionSort() {
for (let i = 1; i < bars.length; i++) {
let j = i;
while (j > 0 && bars[j].offsetHeight < bars[j - 1].offsetHeight) {
await swap(j, j - 1);
j--;
}
}
}
async function mergeSortHelper(left, right) {
if (left >= right) return;
let mid = Math.floor((left + right) / 2);
await mergeSortHelper(left, mid);
await mergeSortHelper(mid + 1, right);
let temp = [];
let i = left, j = mid + 1;
while (i <= mid && j <= right) {
temp.push(bars[i].offsetHeight <= bars[j].offsetHeight ? bars[i++].offsetHeight : bars[j++].offsetHeight);
}
while (i <= mid) temp.push(bars[i++].offsetHeight);
while (j <= right) temp.push(bars[j++].offsetHeight);
for (let k = left; k <= right; k++) {
bars[k].style.height = `${temp[k - left]}px`;
swapCount++;
updateSwapCount();
await new Promise(resolve => setTimeout(resolve, 50));
}
}
async function mergeSort() {
swapCount = 0;
updateSwapCount();
await mergeSortHelper(0, bars.length - 1);
}
async function quickSort(start = 0, end = bars.length - 1) {
if (start < end) {
const pivotIndex = await partition(start, end);
await quickSort(start, pivotIndex - 1);
await quickSort(pivotIndex + 1, end);
}
}
async function partition(start, end) {
const pivot = bars[end].offsetHeight;
let pivotIndex = start;
for (let i = start; i < end; i++) {
if (bars[i].offsetHeight < pivot) {
await swap(i, pivotIndex);
pivotIndex++;
}
}
await swap(pivotIndex, end);
return pivotIndex;
}
function toggleDarkMode() {
document.body.classList.toggle("dark-mode");
}
generateArray();
</script>
</body>
</html>