-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectionSort.html
608 lines (527 loc) · 23.3 KB
/
selectionSort.html
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Selection Sort</title>
<meta name="description" content="Selection sort visualized step by step with its time and space complexities" />
<meta name="keywords"
content="selection sort, selection sort visualized,step by step, selection sort in easy way, time complexity, space complexity, selection sort properties, arun neupane,sorting" />
<link rel="icon" href="./images/sort.png" type="image/x-icon">
<link rel="stylesheet" href="./styles.css">
<link rel="manifest" href="./manifest.json">
<!-- For ios -->
<link rel="apple-touch-icon" href="./images/sort.png">
<meta name="apple-mobile-web-app-status-bar" content="1338BE">
<style>
.least {
background-color: green;
color: white;
transform: scale(1);
}
</style>
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-clike.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-c.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-cpp.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-java.min.js"></script>
</head>
<body>
<nav>
<div class="menu-toggle" onclick="toggleMenu()">
<span></span>
<span></span>
<span></span>
</div>
<div class="nav-links">
<a href="./index.html">Home</a>
<a href="./bubbleSort.html">Bubble <span class="sort-text">sort</span></a>
<a href="./selectionSort.html" class="bb">Selection <span class="sort-text">sort</span></a>
<a href="./insertionSort.html">Insertion <span class="sort-text">sort</span></a>
<a href="./mergeSort.html">Merge <span class="sort-text">sort</span></a>
<a href="./quickSort.html">Quick <span class="sort-text">sort</span></a>
<a href="./heapSort.html">Heap <span class="sort-text">sort</span></a>
<a href="./radixSort.html">Radix <span class="sort-text">sort</span></a>
<a href="./shellSort.html">Shell <span class="sort-text">sort</span></a>
<a href="./summary.html">Summary</a>
<a id="install-button-mobile">Install</a>
</div>
</nav>
<div class="cursor big"></div>
<div class="cursor small"></div>
<div id="context-menu" class="hidden">
<ul>
<li onclick="window.location.reload(true);">Refresh</li>
<li onclick="window.open('./contact.html','_self')">
Contact
</li>
<li>
Share
<ul class="submenu">
<li class="submenu-item">
<a href="https://www.facebook.com/sharer/sharer.php?u=https://easysorting.netlify.app"e=Hey%20check%20out%20this%20sorting%20algorithm%20visualizer"
target="_blank" rel="noopener">
<img src="./images/svg-icons/facebook.svg" alt="Facebook" class="icon">
</a>
</li>
<li class="submenu-item">
<a href="https://www.instagram.com" target="_blank" rel="noopener">
<img src="./images/svg-icons/instagram.svg" alt="Instagram" class="icon">
</a>
</li>
<li class="submenu-item">
<a href="https://wa.me/?text=Hey%20check%20out%20this%20sorting%20algorithm%20visualizer%20-%20https://easysorting.netlify.app"
target="_blank" rel="noopener">
<img src="./images/svg-icons/whatsapp.svg" alt="Whatsapp" class="icon">
</a>
</li>
<li class="submenu-item">
<a href="https://t.me/share/url?url=https://easysorting.netlify.app&text=Hey%20check%20out%20this%20sorting%20algorithm%20visualizer"
target="_blank" rel="noopener">
<img src="./images/svg-icons/telegram.svg" alt="Telegram" class="icon">
</a>
</li>
</ul>
</li>
<li>
Follow me
<ul class="submenu">
<li class="submenu-item">
<a href="https://www.facebook.com/arunneupane9000/" target="_blank" rel="noopener">
<img src="./images/svg-icons/facebook.svg" alt="Facebook" class="icon">
</a>
</li>
<li class="submenu-item">
<a href="https://www.instagram.com/arundada9000" target="_blank" rel="noopener">
<img src="./images/svg-icons/instagram.svg" alt="Instagram" class="icon">
</a>
</li>
<li class="submenu-item">
<a href="https://www.github.com/arundada9000" target="_blank" rel="noopener">
<img src="./images/svg-icons/github.svg" alt="Telegram" class="icon">
</a>
</li>
<li class="submenu-item">
<a href="https://www.youtube.com/@code_with_ease" target="_blank" rel="noopener">
<img src="./images/svg-icons/youtube.svg" alt="Youtube" class="icon">
</a>
</li>
</ul>
</li>
<li onclick="window.open('./feedback.html','_self')">
Feedback
</li>
<li id="installButton">Install</li>
</ul>
</div>
<div class="progress-bar-container">
<div class="progress-bar"></div>
</div>
<div class="container">
<section class="front">
<div class="front-main">
<div class="icon-container">
<img src="./images/selection.webp" alt="Selection sort">
<h1>Selection Sort</h1>
</div>
<div class="sort-input">
<input type="text" id="input-num" label="Enter numbers" title="Enter numbers separated with commas"
placeholder="Enter numbers separated with commas as 16,15,15,10,1">
<div class="buttons-div">
<div class="sort-btn" id="generate-random-btn" title="Generate random numbers button"
onclick="randomGenerate();">
<img src="./images/magic.png" alt="Generate Random Numbers" title="Generate random numbers">
</div>
<button id="sort-btn" class="sort-btn">Sort</button>
</div>
</div>
</div>
<div class="buttons-div"><button id="fast-btn" class="control-btn">Fast Visualization</button>
<button id="skip" class="control-btn">Skip Visualization</button>
</div>
<div class="passesandinfo">
<div class="numbers" id="num-div"></div>
<div class="passes" id="passes"></div>
</div>
</section>
<div class="howto-wrapper scroll-animate">
<div class="howto" id="howto">
<div class="qs">
<h3>How to use ?</h3>
<button class="show">+</button>
<button class="hide">-</button>
</div>
<div class="ans">
<p>
Enter an array separated by commas as : 16,15,15,10,1 or just press the hand icon to generate a
random array then press enter or Sort button. Sorting
will
start
and
you can watch each pass as it gets sorted.
When it shows green it means the two of them are
being
compared
when red shows, it means the left number is greater than right number so it needs swapping
The blue color means the number is
at its
correct position and thus sorted
it will continue to check other numbers until it reaches end.
</p>
</div>
</div>
</div>
<div class="sorting-algorithm-container">
<h1 class="algorithm-title scroll-animate">Selection Sort Overview</h1>
<section class="history scroll-animate">
<h2 class="section-title">History</h2>
<p>Selection sort has been around since the early days of computer programming. It was developed as
a
simple, intuitive algorithm for sorting data. The concept has been used in various forms across
multiple programming languages since the 1960s.</p>
</section>
<section class="current-uses scroll-animate">
<h2 class="section-title">Current Uses</h2>
<ul>
<li><strong>Small Data Sets:</strong> Efficient for small datasets where performance is not
critical.</li>
<li><strong>Memory Constraints:</strong> Often used in systems with limited memory resources.
</li>
<li><strong>Selection Problems:</strong> Sometimes applied in selection algorithms where the
smallest or largest element is needed.</li>
</ul>
</section>
<section class="complexity scroll-animate">
<h2 class="section-title">Time and Space Complexity</h2>
<h3>Time Complexity</h3>
<ul>
<li><strong>Best Case:</strong> <code>O(n<sup>2</sup>)</code> – Regardless of the initial
arrangement.
</li>
<li><strong>Average Case:</strong> <code>O(n<sup>2</sup>)</code> – Quadratic complexity for average
cases.
</li>
<li><strong>Worst Case:</strong> <code>O(n<sup>2</sup>)</code> – Quadratic complexity for worst
cases.
</li>
</ul>
<h3>Space Complexity</h3>
<p><strong>Space Complexity:</strong> <code>O(1)</code> – It is an in-place sorting algorithm
requiring
a constant amount of additional space.</p>
</section>
<section class="pseudo-code scroll-animate">
<h2 class="section-title">Pseudo Code</h2>
<div class="code-container">
<button class="copy-btn" onclick="copyToClipboard(this)">Copy</button>
<pre class="pseudo-code-code"><code><!-- This is the pseudo code for Selection Sort -->function selectionSort(arr):
n = length(arr)
for i from 0 to n-1:
minIndex = i
for j from i+1 to n:
if arr[j] < arr[minIndex]:
minIndex = j
/* Swap the found minimum element with the first element */
temp = arr[i]
arr[i] = arr[minIndex]
arr[minIndex] = temp
return arr</code></pre>
</div>
</section>
<section class="characteristics scroll-animate">
<h2 class="section-title">Key Characteristics</h2>
<ul>
<li><strong>Stability:</strong> Selection sort is not a stable sorting algorithm.</li>
<li><strong>Adaptability:</strong> It does not adapt well to varying input patterns.</li>
</ul>
</section>
<section class="summary scroll-animate">
<h2 class="section-title">Summary</h2>
<p>Selection sort is a simple algorithm that divides the input list into a sorted and an unsorted
region. Despite its <code>O(n<sup>2</sup>)</code> complexity, its simplicity makes it useful for
small
datasets and educational purposes.</p>
</section>
<section class="language-code-container">
<h1 class="scroll-animate">Selection Sort Code Implementation</h1>
<div class="language-selector scroll-animate">
<button data-language="c">C</button>
<button data-language="cpp">C++</button>
<button data-language="javascript">JavaScript</button>
<button data-language="python">Python</button>
<button data-language="java">Java</button>
</div>
<pre class="inside-code-container">
<code id="code-display"></code>
</pre>
<button class="language-copy-button scroll-animate">Copy</button>
</section>
</div>
</div>
<button id="backToTopBtn" title="Go to top">↑</button>
<footer class="site-footer">
<div class="footer-container">
<p class="footer-title">Sorting Visualizer</p>
<div class="footer-links">
<a href="./about.html">About</a> |
<a href="./feedback.html">Feedback </a> |
<a href="./contact.html">Contact</a>
</div>
<p class="footer-info">© <span id="year">2024</span> Sorting Visualizer. All rights reserved.</p>
<p class="footer-credit">Made with ❤️ by Arun Neupane</p>
<p class="footer-credit">For Learning and Development</p>
</div>
</footer>
<script src="./app.js"></script>
<script>
const codeSnippets = {
javascript: `// JavaScript code for Selection Sort
function selectionSort(arr) {
for (let i = 0; i < arr.length - 1; i++) {
let minIdx = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
// Swap the found minimum element with the first element
let temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
}
let arr = [16, 15, 15, 10, 1];
console.log('Before sorting:', arr);
selectionSort(arr);
console.log('After sorting:', arr);`,
c: `// C code for Selection Sort
#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[] = {16, 15, 15, 10, 1};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Before sorting: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
selectionSort(arr, n);
printf("\\nAfter sorting: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}`,
cpp: `// C++ code for Selection Sort
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[] = {16, 15, 15, 10, 1};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Before sorting: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
selectionSort(arr, n);
cout << "\\nAfter sorting: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}`,
java: `// Java code for Selection Sort
public class SortingAlgorithm {
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
}
public static void main(String[] args) {
int[] arr = {16, 15, 15, 10, 1};
System.out.print("Before sorting: ");
for (int num : arr) {
System.out.print(num + " ");
}
selectionSort(arr);
System.out.print("\\nAfter sorting: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
}`,
python: `# Python code for Selection Sort
def selectionSort(arr):
for i in range(len(arr) - 1):
minIdx = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[minIdx]:
minIdx = j
# Swap the found minimum element with the first element
arr[i], arr[minIdx] = arr[minIdx], arr[i]
arr = [16, 15, 15, 10, 1]
print('Before sorting:', arr)
selectionSort(arr)
print('After sorting:', arr)
`
};
const sortBtn = document.getElementById("sort-btn");
const inputBox = document.getElementById("input-num");
const passesDiv = document.getElementById("passes");
const howto = document.getElementById("howto");
const skipBtn = document.getElementById("skip");
const fastBtn = document.getElementById("fast-btn");
let ms = 1000;
sortBtn.addEventListener("click", () => {
if (sortBtn.textContent == "Sorting...") return;
let input = inputBox.value;
if (input == "") {
alert("Please enter some values ! ");
return;
}
ms = 1000;
displayBtn("inline");
sortBtn.textContent = "Sorting...";
const values = input.split(',');
const numDiv = document.getElementById("num-div");
numDiv.innerHTML = "";
passesDiv.innerHTML = "";
howto.innerHTML = "";
howto.style.visibility = "hidden";
for (let i = 0; i < values.length; i++) {
let el = document.createElement("div");
el.className = "num-wrapper";
let li = document.createElement("div");
li.className = "num";
li.textContent = values[i];
values[i] = parseFloat(values[i]);
let li1 = document.createElement("p");
li1.classList = "ij";
li1.textContent = i;
el.appendChild(li1);
el.appendChild(li);
numDiv.appendChild(el);
}
const divs = document.querySelectorAll(".num");
selectionSort();
async function selectionSort() {
let p, least, i, j;
for (i = 0; i < values.length; i++) {
least = values[i];
p = i;
divs[i].classList.add("comparing");
await sleep(ms);
for (j = i + 1; j < values.length; j++) {
divs[j].classList.add("swapping");
if (values[j] < least) {
divs[i].classList.remove("comparing");
divs[i].classList.add("least");
divs[p].classList.remove("swapping");
await sleep(ms);
least = values[j];
p = j;
divs[j].classList.add("swapping");
continue;
}
await sleep(ms);
divs[j].classList.remove("swapping");
}
if (values[i] != least) {
values[p] = values[i];
values[i] = least;
divs[i].classList.remove("least");
divs[i].classList.add("swapping");
await sleep(ms);
divs[p].textContent = values[p];
divs[i].textContent = least;
}
await sleep(ms);
divs[i].classList.remove("comparing");
divs[i].classList.remove("least");
divs[i].classList.remove("swapping");
divs[p].classList.remove("swapping");
divs[i].classList.add("sorted");
let passCount = i + 1;
let passes = createDiv("numbers", "num", values, values.length, j, passCount);
passesDiv.appendChild(passes);
}
sortedArray = values.join(", ");
howto.style.visibility = "visible";
howto.textContent = "The array is successfully sorted.There were " + values.length + " numbers and it took " + (values.length - 1) + " passes.The sorted array is : " + sortedArray;
const btn = document.createElement("button");
btn.textContent = "Copy";
btn.className = "copy-btn";
btn.id = "ans-copy-btn";
btn.addEventListener("click", () => {
navigator.clipboard.writeText(sortedArray);
btn.textContent = "Copied!";
setTimeout(() => {
btn.textContent = "Copy";
}, 2000);
});
howto.appendChild(btn);
displayBtn("none");
sortBtn.textContent = "Sort";
}
});
function createDiv(cn1, cn2, content, time, j, passCount) {
const outerDiv = document.createElement("div");
outerDiv.className = cn1;
const li = document.createElement("div");
li.className = cn2;
li.textContent = (passCount == time) ? "Sorted" : "Pass " + passCount;
outerDiv.appendChild(li);
for (let i = 0; i < time; i++) {
const li = document.createElement("div");
li.className = cn2;
if (i < passCount)
li.classList.add("sorted");
li.textContent = content[i];
outerDiv.appendChild(li);
}
return outerDiv;
}
function toggleClass(divs, i, j, cn) {
divs[j].classList.toggle(cn);
divs[i].classList.toggle(cn);
}
</script>
<script src="./script.js"></script>
</body>
</html>