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

Function CountingSort

assets/js/countingsort.js:63–133  ·  view source on GitHub ↗
(delay = 250)

Source from the content-addressed store, hash-verified

61
62// Asynchronous CountingSort function
63async function CountingSort(delay = 250) {
64var blocks = document.querySelectorAll(".block");
65
66// To store frequency of every block
67for (var i = 0; i < blocks.length; i += 1) {
68 //To highlight the current traversed block
69 blocks[i].style.backgroundColor = "#FF4949";
70
71 //Extracting the value of current block
72 var value = Number(blocks[i].childNodes[0].innerHTML);
73
74 var freq_array = document.getElementsByClassName("block_id3");
75
76 freq_array[value - 1].innerText++;
77
78 // To wait for .1 sec
79 await new Promise((resolve) =>
80 setTimeout(() => {
81 resolve();
82 }, delay)
83 );
84
85 //Changing to previous color
86 blocks[i].style.backgroundColor = "#00a2ff";
87}
88
89//Sorting by using frequency array
90var idx = 0;
91for (var i = 0; i < blocks.length; i += 1) {
92 var freq = document.getElementsByClassName("block_id3");
93
94 var temp = Number(freq[i].innerText);
95
96 var freq_block = document.getElementsByClassName("block2");
97
98 //changing color of freq block
99 freq_block[i].style.backgroundColor = "#FF4949";
100
101 // To wait for .1 sec
102 await new Promise((resolve) =>
103 setTimeout(() => {
104 resolve();
105 }, 2 * delay)
106 );
107
108 if (temp == 0) {
109 //changing color of freq block to previous one
110 freq_block[i].style.backgroundColor = "darkgray";
111 continue;
112 }
113
114 var block_label = document.getElementsByClassName("block_id");
115
116 //sorting the block array
117 for (var j = 0; j < temp; j++) {
118 blocks[idx].style.height = `${(i + 1) * 13}px`;
119 block_label[idx].innerText = i + 1;
120 idx++;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected