I'm trying to visualize the selection sort algorithm. The movements start working properly and the array elements reverse, but suddenly, for the last iterations, everything turns into a mess as shown in the picture. I can't figure out the problem.
async function selectionSort(main_arr) {
for (var i=0; i<main_arr.length; i++)
{
var min_ind=i;
for(var j=i+1; j<main_arr.length; j++)
{
if(main_arr[min_ind]>main_arr[j])
{
min_ind=j;
}
}
if(min_ind!=i)
{
var temp=main_arr[min_ind];
main_arr[min_ind]=main_arr[i];
main_arr[i]=temp;
var offbig=$('.slot'+min_ind).offset().left,offsmall=$('.slot'+i).offset().left;
$(".slot"+min_ind).animate(
{
right:offbig-offsmall
}
,500);
$(".slot"+i).animate(
{
left:offbig-offsmall
}
,500);
await sleep(1000);
}
}
}
