Im trying to create a Sort Algorithm Visualizer in React. This function is working but I want to slow down the for-loop so the state sets every 400 milliseconds.
bubbleSort = (arr) => {
console.log('bubblesort is running');
var len = arr.length;
console.log('array length: ', len);
console.log(arr);
for (var i = len-1; i>=0; i--){
console.log("i: ", i);
for(var j = 1; j<=i; j++){
console.log("j: ", j);
if(arr[j-1]>arr[j]){
var temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
console.log("current array: ", arr);
this.setState({
array: arr
})
}
}
}
console.log("final array: ", arr)
return arr
}