-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradixSort.html
867 lines (734 loc) · 30.7 KB
/
radixSort.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Radix sort visualized step by step with its time and space complexities" />
<meta name="keyword"
content="radix sort, radix sort visualized,step by step, radix sort in easy way, time complexity, space complexity, radix sort properties, arun neupane,sorting" />
<title>Radix Sort</title>
<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>
.max-container {
display: none;
}
.passes #countof-div .num-wrapper:nth-child(2) {
display: none !important;
}
.passes .numbers {
padding-bottom: 1rem;
border: none;
}
.passes .numbers .num:first-child {
width: 90px;
color: #000000;
}
</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">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" class="bb">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/radix.webp" alt="Radix Sort">
<h1>Radix 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="max-container info" id="max-container"><span id="max"></span>
<span id="info-about-sorting" class="info"></span>
</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">Radix Sort Overview</h1>
<section class="history scroll-animate">
<h2 class="section-title">History</h2>
<p>Radix sort was developed in the 1960s and is a non-comparison-based sorting algorithm. It sorts
numbers by processing individual digits. Radix sort is efficient for sorting large sets of integers
and has been utilized in various applications.</p>
</section>
<section class="current-uses scroll-animate">
<h2 class="section-title">Current Uses</h2>
<ul>
<li><strong>Large Integer Sets:</strong> Commonly used to sort large datasets of integers.</li>
<li><strong>Specific Applications:</strong> Utilized in applications that require sorting of
fixed-length strings or numbers.</li>
<li><strong>Counting Sort Combination:</strong> Often used in combination with counting sort for
efficiency.</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(nk)</code> – Where <code>n</code> is the number of elements
and <code>k</code> is the number of digits in the largest number.</li>
<li><strong>Average Case:</strong> <code>O(nk)</code> – Efficiency depends on the number of digits.
</li>
<li><strong>Worst Case:</strong> <code>O(nk)</code> – Same as the average case.</li>
</ul>
<h3>Space Complexity</h3>
<p><strong>Space Complexity:</strong> <code>O(n + k)</code> – Requires additional space for the output
array and counting array.</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 Bubble Sort -->function radixSort(array)
maxNumber = findMax(array)
numberOfDigits = countDigits(maxNumber)
for digit in 1 to numberOfDigits do
applyCountingSort(array, digit)
end for
end function
function applyCountingSort(array, digitPosition)
create 10 buckets to represent each digit (0 to 9)
for each element in array do
digit = getDigit(element, digitPosition)
place element into corresponding bucket based on digit
end for
merge all buckets in order to form the sorted array for this digit position
end function
function getDigit(number, position)
return (number / 10^(position - 1)) % 10
end function
function findMax(array)
maxNumber = array[0]
for i = 1 to array.length - 1 do
if array[i] > maxNumber then
maxNumber = array[i]
end if
end for
return maxNumber
end function
function countDigits(number)
count = 0
while number > 0 do
number = number / 10
count = count + 1
end while
return count
end function</code></pre>
</div>
</section>
<section class="characteristics scroll-animate">
<h2 class="section-title">Key Characteristics</h2>
<ul>
<li><strong>Non-Comparison Sort:</strong> Radix sort does not compare elements directly.</li>
<li><strong>Stable:</strong> Radix sort can be implemented as a stable sorting algorithm.</li>
</ul>
</section>
<section class="summary scroll-animate">
<h2 class="section-title">Summary</h2>
<p>Radix sort is an efficient non-comparison sorting algorithm that processes individual digits to sort
integers. Its time complexity of <code>O(nk)</code> makes it suitable for sorting large sets of
data, particularly when the range of digits is small.</p>
</section>
<section class="language-code-container">
<h1 class="scroll-animate">Radix 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 Radix Sort
function getMax(arr) {
return Math.max(...arr);
}
function countingSort(arr, exp) {
const output = new Array(arr.length);
const count = new Array(10).fill(0);
for (let i = 0; i < arr.length; i++) {
const index = Math.floor(arr[i] / exp) % 10;
count[index]++;
}
for (let i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (let i = arr.length - 1; i >= 0; i--) {
const index = Math.floor(arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (let i = 0; i < arr.length; i++) {
arr[i] = output[i];
}
}
function radixSort(arr) {
const max = getMax(arr);
for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
countingSort(arr, exp);
}
}
let arr = [16, 15, 15, 10, 1];
console.log('Before sorting:', arr);
radixSort(arr);
console.log('After sorting:', arr);`,
c: `// C code for Radix Sort
#include <stdio.h>
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
void countingSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
for (int i = 0; i < n; i++) {
int index = (arr[i] / exp) % 10;
count[index]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
int index = (arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
void radixSort(int arr[], int n) {
int max = getMax(arr, n);
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSort(arr, n, exp);
}
}
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]);
}
radixSort(arr, n);
printf("\\nAfter sorting: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}`,
cpp: `// C++ code for Radix Sort
#include <iostream>
#include <vector>
using namespace std;
int getMax(vector<int>& arr) {
int max = arr[0];
for (int i = 1; i < arr.size(); i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
void countingSort(vector<int>& arr, int exp) {
vector<int> output(arr.size());
vector<int> count(10, 0);
for (int i = 0; i < arr.size(); i++) {
int index = (arr[i] / exp) % 10;
count[index]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = arr.size() - 1; i >= 0; i--) {
int index = (arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (int i = 0; i < arr.size(); i++) {
arr[i] = output[i];
}
}
void radixSort(vector<int>& arr) {
int max = getMax(arr);
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSort(arr, exp);
}
}
int main() {
vector<int> arr = {16, 15, 15, 10, 1};
cout << "Before sorting: ";
for (int num : arr) {
cout << num << " ";
}
radixSort(arr);
cout << "\\nAfter sorting: ";
for (int num : arr) {
cout << num << " ";
}
return 0;
}`,
java: `// Java code for Radix Sort
public class SortingAlgorithm {
public static int getMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public static void countingSort(int[] arr, int n, int exp) {
int[] output = new int[n];
int[] count = new int[10];
for (int i = 0; i < n; i++) {
int index = (arr[i] / exp) % 10;
count[index]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
int index = (arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
public static void radixSort(int[] arr) {
int max = getMax(arr);
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSort(arr, arr.length, exp);
}
}
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 + " ");
}
radixSort(arr);
System.out.print("\\nAfter sorting: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
}`,
python: `# Python code for Radix Sort
def getMax(arr):
return max(arr)
def countingSort(arr, exp):
output = [0] * len(arr)
count = [0] * 10
for i in range(len(arr)):
index = (arr[i] // exp) % 10
count[index] += 1
for i in range(1, 10):
count[i] += count[i - 1]
for i in range(len(arr) - 1, -1, -1):
index = (arr[i] // exp) % 10
output[count[index] - 1] = arr[i]
count[index] -= 1
for i in range(len(arr)):
arr[i] = output[i]
def radixSort(arr):
max_val = getMax(arr)
exp = 1
while max_val // exp > 0:
countingSort(arr, exp)
exp *= 10
arr = [16, 15, 15, 10, 1]
print('Before sorting:', arr)
radixSort(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, ind = 0;
sortBtn.addEventListener("click", () => {
if (sortBtn.textContent == "Sorting...") return;
let input = inputBox.value.trim();
if (input == "") {
alert("Please enter some values ! ");
return;
}
ms = 1000;
ind = 0;
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");
radixSort();
async function radixSort() {
const max = getMax();
for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
if (Math.floor(max / (exp * 10)) === 0) {
await countingSort(exp, true);
} else {
await countingSort(exp, false);
}
}
sortedArray = values.join(", ");
howto.style.visibility = "visible";
document.getElementById("max").textContent = "Array is Sorted";
document.getElementById("info-about-sorting").textContent = "";
howto.innerHTML = "The array is successfully sorted.There were " + values.length + " numbers." + "The sorted array is : <br>" + 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 getMax() {
let max = values[0];
for (let i = 1; i < values.length; i++) {
if (values[i] > max) {
max = values[i];
}
}
displayMax(max);
return max;
}
async function countingSort(exp, isSorted) {
let output = new Array(values.length).fill(0);
let count = new Array(10).fill(0);
let info;
switch (exp) {
case 1:
info = infoAboutPasses("Sorting for 1s place ...", "info");
break;
case 10:
info = infoAboutPasses("Sorting for 10s place ...", "info");
break;
case 100:
info = infoAboutPasses("Sorting for 100s place ...", "info");
break;
case 1000:
info = infoAboutPasses("Sorting for 1000s place ...", "info");
break;
case 10000:
info = infoAboutPasses("Sorting for 10000s place ...", "info");
break;
case 100000:
info = infoAboutPasses("Sorting for 100000s place ...", "info");
break;
case 1000000:
info = infoAboutPasses("Sorting for 100000s place ...", "info");
break;
}
passesDiv.appendChild(info);
let passes = createDiv("numbers", "num", "Count Occurrence", count.length, "count-div");
passesDiv.appendChild(passes);
const countDiv = document.querySelectorAll("#passes #count-div .num");
for (let i = 0; i < count.length; i++) {
countDiv[ind * 10 + ind + i + 1].textContent = 0;
}
for (let i = 0; i < values.length; i++) {
const index = Math.floor(values[i] / exp) % 10;
count[index]++;
toggleClass("comparing", divs[i], countDiv[ind * 10 + ind + index + 1]);
await sleep(ms);
countDiv[ind * 10 + ind + index + 1].textContent = count[index];
toggleClass("comparing", divs[i], countDiv[ind * 10 + ind + index + 1]);
await sleep(ms);
}
// throw new Error("Stopping the program execution.");
passes = createDiv("numbers", "num", "Count", count.length, "countof-div");
passesDiv.appendChild(passes);
const countOf = document.querySelectorAll("#passes #countof-div .num");
for (let i = 1; i < 10; i++) {
count[i] += count[i - 1];
if (i == 1) {
toggleClass("comparing", countDiv[ind * 10 + ind + i], countDiv[ind * 10 + ind + i + 1]);
}
else {
toggleClass("comparing", countOf[ind * 10 + ind + i], countDiv[ind * 10 + ind + i + 1]);
}
toggleClass("swapping", countOf[ind * 10 + ind + i + 1]);
await sleep(ms);
countOf[ind * 10 + ind + i + 1].textContent = count[i];
await sleep(ms);
if (i == 1) {
toggleClass("comparing", countDiv[ind * 10 + ind + i], countDiv[ind * 10 + ind + i + 1]);
}
else {
toggleClass("comparing", countOf[ind * 10 + ind + i], countDiv[ind * 10 + ind + i + 1]);
}
toggleClass("swapping", countOf[ind * 10 + ind + i + 1]);
await sleep(ms);
}
passes = createDiv("numbers", "num", "Sorted", values.length, "sorted-div");
passesDiv.appendChild(passes);
const sortedArray = document.querySelectorAll("#passes #sorted-div .num");
for (let i = values.length - 1; i >= 0; i--) {
const index = Math.floor(values[i] / exp) % 10;
output[count[index] - 1] = values[i];
count[index]--;
}
for (let i = 0; i < values.length; i++) {
values[i] = output[i];
sortedArray[ind * values.length + ind + i + 1].textContent = values[i];
divs[i].textContent = values[i];
await sleep(ms);
}
if (isSorted) {
for (let i = 0; i < values.length; i++) {
toggleClass("sorted", divs[i]);
toggleClass("sorted", sortedArray[ind * values.length + ind + i + 1]);
}
}
ind++;
}
});
function createDiv(cn1, cn2, dn, l, aid, content) {
const outerDiv = document.createElement("div");
outerDiv.className = cn1;
outerDiv.id = aid;
const lii = document.createElement("div");
lii.className = cn2;
lii.textContent = dn;
outerDiv.appendChild(lii);
for (let i = 0; i < l; i++) {
let el = document.createElement("div");
el.className = "num-wrapper";
let li = document.createElement("div");
li.className = "num";
let li1 = document.createElement("p");
li1.classList = "ij";
li1.textContent = i;
el.appendChild(li1);
el.appendChild(li);
outerDiv.appendChild(el);
}
return outerDiv;
}
function toggleClass(className, ...elements) {
elements.forEach(element => {
if (element instanceof HTMLElement) {
element.classList.toggle(className);
} else {
console.error('Invalid element:', element);
}
});
}
function displayMax(max) {
document.getElementById("max-container").style.display = "block";
document.getElementById("max").textContent = "Max = " + max;
}
function infoAboutPasses(content, cn) {
const li = document.createElement("div");
li.className = cn;
li.textContent = content;
document.getElementById("info-about-sorting").textContent = " | " + content;
return li;
}
</script>
<script src="./script.js"></script>
</body>
</html>