am trying to sort an array using bubble sort and at each step of sort I want to render the change in position of values inside an array. So made the below algorithm with React, at very first time it does render the changes but then gets stop, doesn’t render anything. I tried to console log the array I am rendering in and I am getting the each sort step on console but don’t know why it is not working here. Can anybody please tell me why it’s happening so? also what can be done?
Note: It was working fine and rendering the sorted array initially, but later when I added setTimeOut
function it is not working.
App.js
import React, { useState } from "react";
const App = () => {
const [arrayForSort, setArrayForSort] = useState([44,2,46,11,15,34,1,7,55]);
const fetchArray = arrayForSort.map((element) => {
return <span>{element} </span>;
});
const bubbleSort = (array) => {
let newArray = [...array];
let n = newArray.length;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n - i; j++) {
const myVar = setTimeout(() => {
if (newArray[j] > newArray[j + 1]) {
[newArray[j], newArray[j + 1]] = [newArray[j + 1], newArray[j]];
}
console.log(newArray); // render each step of sort
setArrayForSort(newArray); // on screen it just shows "2 44 46 11 15 34 1 7 55" the very first step of sort
}, 2000);
}
}
};
return (
<div>
<h4>Array: </h4>
<span>{fetchArray}</span>
<div>
<button
onClick={() => {
bubbleSort(arrayForSort);
}}
>
Sort
</button>
</div>
</div>
);
};
export default App;