MCPcopy Create free account
hub / github.com/Vishal-raj-1/Awesome-JavaScript-Projects / SelectionSort

Function SelectionSort

assets/js/selectionsort.js:42–115  ·  view source on GitHub ↗
(delay = 300)

Source from the content-addressed store, hash-verified

40
41// asynchronous function to perform "Selection Sort"
42async function SelectionSort(delay = 300) {
43 let bars = document.querySelectorAll(".bar");
44 // Assign 0 to min_idx
45 var min_idx = 0;
46 for (var i = 0; i < bars.length; i += 1) {
47
48 // Assign i to min_idx
49 min_idx = i;
50
51 // Provide darkblue color to the ith bar
52 bars[i].style.backgroundColor = "darkblue";
53 for (var j = i + 1; j < bars.length; j += 1) {
54
55 // Provide red color to the jth bar
56 bars[j].style.backgroundColor = "red";
57
58 // To pause the execution of code for 300 milliseconds
59 await new Promise((resolve) =>
60 setTimeout(() => {
61 resolve();
62 }, 300)
63 );
64
65 // To store the integer value of jth bar to var1
66 var val1 = parseInt(bars[j].childNodes[0].innerHTML);
67
68 // To store the integer value of (min_idx)th bar to var2
69 var val2 = parseInt(bars[min_idx].childNodes[0].innerHTML);
70
71 // Compare val1 & val2
72 if (val1 < val2) {
73 if (min_idx !== i) {
74
75 // Provide skyblue color to the (min-idx)th bar
76 bars[min_idx].style.backgroundColor = " rgb(24, 190, 255)";
77 }
78 min_idx = j;
79 } else {
80
81 // Provide skyblue color to the jth bar
82 bars[j].style.backgroundColor = " rgb(24, 190, 255)";
83 }
84 }
85
86 // To swap ith and (min_idx)th bar
87 var temp1 = bars[min_idx].style.height;
88 var temp2 = bars[min_idx].childNodes[0].innerText;
89 bars[min_idx].style.height = bars[i].style.height;
90 bars[i].style.height = temp1;
91 bars[min_idx].childNodes[0].innerText = bars[i].childNodes[0].innerText;
92 bars[i].childNodes[0].innerText = temp2;
93
94 // To pause the execution of code for 300 milliseconds
95 await new Promise((resolve) =>
96 setTimeout(() => {
97 resolve();
98 }, 300)
99 );

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected