I have been working on a sorting visualizer and have successfully implemented algorithms like bubble sort, selection sort, and insertion sort. However, I am facing an issue while trying to implement shell sort. The sorting process stops after 3 iterations through the entire array. Everything seems to be running smoothly until it suddenly halts. Could someone help me understand why this is happening? One possibility could be that the chart updates after each iteration of the inner loop. Below is the snippet of my code:
async ShellSort(delay = 5) {
for (let gap = Math.floor(this.data.length / 2); gap > 0; gap /= 2) {
for (let i = gap; i < this.data.length; i++) {
let temp = this.data[i];
let j = i;
while (j > gap && temp < this.data[j - gap]) {
this.data[j] = this.data[j - gap];
j -= gap;
}
this.data[j] = temp;
//update the chart
await new Promise(resolve =>
setTimeout(() => {
resolve();
}, delay)
);
this.draw();
}
}
}