I am trying to create a visualizer for sorting an array of integers. I need to draw the representation after each iteration of the sort and add a delay so that it does not happen instantly and only show the sorted array. However, the setTimeout does not seem to be working and it is just displaying a representation of the sorted array instead of after each iteration. The code is in java script.
function sort(){
var len = arr.length;
for (var i = len-1; i>=0; i--){
for(var j = 1; j<=i; j++){
if(arr[j-1]>arr[j]){
var temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
setTimeout(startDraw, 3000);
}
}
}
}