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

Function InsertionSort

assets/js/insertionsort.js:42–133  ·  view source on GitHub ↗
(delay = 600)

Source from the content-addressed store, hash-verified

40
41// Asynchronous function to perform "Insertion Sort"
42async function InsertionSort(delay = 600) {
43let bars = document.querySelectorAll(".bar");
44
45// Provide lightgreen colour to 0th bar
46bars[0].style.backgroundColor = " rgb(49, 226, 13)";
47for (var i = 1; i < bars.length; i += 1) {
48
49 // Assign i-1 to j
50 var j = i - 1;
51
52 // To store the integer value of ith bar to key
53 var key =
54 parseInt(bars[i].childNodes[0].innerHTML);
55
56 // To store the ith bar height to height
57 var height = bars[i].style.height;
58
59 // For selecting section having id "ele"
60 var barval=document.getElementById("ele")
61
62 // For dynamically Updating the selected element
63 barval.innerHTML=
64 `<h3>Element Selected is :${key}</h3>`;
65
66 //Provide darkblue color to the ith bar
67 bars[i].style.backgroundColor = "darkblue";
68
69 // To pause the execution of code for 600 milliseconds
70 await new Promise((resolve) =>
71 setTimeout(() => {
72 resolve();
73 }, 600)
74);
75
76 // For placing selected element at its correct position
77 while (j >= 0 && parseInt(bars[j].childNodes[0].innerHTML) > key) {
78
79 // Provide darkblue color to the jth bar
80 bars[j].style.backgroundColor = "darkblue";
81
82 // For placing jth element over (j+1)th element
83 bars[j + 1].style.height = bars[j].style.height;
84 bars[j + 1].childNodes[0].innerText =
85 bars[j].childNodes[0].innerText;
86
87 // Assign j-1 to j
88 j = j - 1;
89
90 // To pause the execution of code for 600 milliseconds
91 await new Promise((resolve) =>
92 setTimeout(() => {
93 resolve();
94 }, 600)
95 );
96
97 // Provide lightgreen color to the sorted part
98 for(var k=i;k>=0;k--){
99 bars[k].style.backgroundColor = " rgb(49, 226, 13)";

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected