选择排序 Javascript 动画

selectionSort Javascript animation

我正在尝试构建 javascript 中一些著名排序算法的可视化表示,但我不明白为什么我的代码不打印每次迭代,即使打印函数在 for环形。我只得到最后的结果。

这是排序函数,特别是选择排序算法:

function selectionSort(array) {
var i, j, min_idx;
let n = array.length;

for (i = 0; i < n-1; i++)
{
    min_idx = i;
    for (j = i + 1; j < n; j++)
    {
        if (array[j] < array[min_idx])
        {
            min_idx = j;
        }
    }
    var temp = array[min_idx];
    array[min_idx] = array[i];
    array[i] = temp;
    printArray(array);
}

}

这是打印功能:

function printArray(array) {
document.getElementById('container').innerHTML ='';
for(let i = 0; i < array.length; i++)
{
    document.getElementById('container').innerHTML += '<div class = "column '+i+'" id = "'+i+'" style = "height: '+array[i]+'px;"></div>';
}

}

非常感谢

这是@Bravo 在评论中所说的。屏幕每秒至少更新 60 次,但排序所需的时间更少。所以你需要在递归循环中添加一个超时,这样你才能真正看到动画。

我用这个递归循环替换了第一个 for 循环。我认为代码不言自明。

我在你的 printArray() 中做了一些优化,其中不断进行 DOM 更改需要时间。相反,循环创建一个文本字符串,然后将其添加一次到 #container.innerHTML。你给可视化div的值也有一些错误的想法,你只添加了订单(i),而不是添加实际值(array[i])。

const iterationLegend = document.getElementById('iterations');
const containerDiv    = document.getElementById('container');

const ANIMATION_SPEED = 1000;
const RESTART = 0;

var firstIteration;

function startSelectionSort(array) {
  firstIteration = RESTART;
  selectionSort(array);
}

function selectionSort(array) {
  let min_idx = firstIteration,
      n = array.length;
  
  for (let j = firstIteration + 1; j < n; j++) {
    if (array[j] < array[min_idx]) {
      min_idx = j;
    }
  }

  var temp = array[min_idx];
  array[min_idx] = array[firstIteration];
  array[firstIteration] = temp;

  visualizeArray(array);
  iterationLegend.textContent = 'iteration ' + firstIteration;
    
  if (firstIteration < n - 1) {
    firstIteration++;
    setTimeout(selectionSort.bind(this, array), ANIMATION_SPEED);
  } else {
    iterationLegend.textContent = 'Done';
  }
}

function visualizeArray(array) {
  let elementStr = '';
  let value = 0;

  for (let i = 0; i < array.length; i++) {
    value = array[i];
    elementStr += `<div class="column${value}" data-value="${value}"></div>`;
  }

  containerDiv.innerHTML = elementStr;
}

startSelectionSort([2, 3, 5, 5, 1, 1, 1, 1, 4]);
fieldset {
  display: inline;
}

#iterations {
  font-size: 13px;
  text-transform: uppercase;
}

#container {
  display: inline-flex;
}

#container > div {
  width: 10px;
  height: 100px;
  margin: 2px 1px 0px;
}

.column1 {
  background-color: brown;
}

.column2 {
  background-color: black;
}

.column3 {
  background-color: teal;
}

.column4 {
  background-color: red;
}

.column5 {
  background-color: indigo;
}
<fieldset>
 <legend id="iterations">Iterations</legend>
 <div id="container"></div>
</fieldset>