| 50 | } |
| 51 | |
| 52 | async function InsertionSort(clsnam, delay = 600) { |
| 53 | let blocks = document.getElementsByClassName(clsnam); |
| 54 | blocks[0].style.backgroundColor = "rgb(49, 226, 13)"; |
| 55 | |
| 56 | for (var i = 1; i < blocks.length; i += 1) { |
| 57 | var j = i - 1; |
| 58 | |
| 59 | // To store the integer value of ith block to key |
| 60 | var key = parseInt(blocks[i].childNodes[0].innerHTML); |
| 61 | |
| 62 | // To store the ith block height to height |
| 63 | var height = blocks[i].style.height; |
| 64 | |
| 65 | // Provide darkblue color to the ith block |
| 66 | blocks[i].style.backgroundColor = "darkblue"; |
| 67 | |
| 68 | // To pause the execution of code for 600 milliseconds |
| 69 | await new Promise((resolve) => |
| 70 | setTimeout(() => { |
| 71 | resolve(); |
| 72 | }, 600) |
| 73 | ); |
| 74 | |
| 75 | // For placing selected element at its correct position |
| 76 | while (j >= 0 && parseInt(blocks[j].childNodes[0].innerHTML) > key) { |
| 77 | |
| 78 | // Provide darkblue color to the jth block |
| 79 | blocks[j].style.backgroundColor = "darkblue"; |
| 80 | |
| 81 | // For placing jth element over (j+1)th element |
| 82 | blocks[j + 1].style.height = blocks[j].style.height; |
| 83 | blocks[j + 1].childNodes[0].innerText = |
| 84 | blocks[j].childNodes[0].innerText; |
| 85 | j = j - 1; |
| 86 | |
| 87 | // To pause the execution of code for 600 milliseconds |
| 88 | await new Promise((resolve) => |
| 89 | setTimeout(() => { |
| 90 | resolve(); |
| 91 | }, delay) |
| 92 | ); |
| 93 | |
| 94 | // Provide lightgreen color to the sorted part |
| 95 | for (var k = i; k >= 0; k--) { |
| 96 | blocks[k].style.backgroundColor = " rgb(49, 226, 13)"; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Placing the selected element to its correct position |
| 101 | blocks[j + 1].style.height = height; |
| 102 | blocks[j + 1].childNodes[0].innerHTML = key; |
| 103 | |
| 104 | // To pause the execution of code for 600 milliseconds |
| 105 | await new Promise((resolve) => |
| 106 | setTimeout(() => { |
| 107 | resolve(); |
| 108 | }, delay) |
| 109 | ); |