JSFiddle - React, Tailwind, and code Playground

by Abhishek Kumar

HTML

<div id="display">

</div>

JavaScript

async function insertionSort(ilist) {
  let list = [...ilist];
	await log(list);
  for (let j = 1; j < list.length; j++) {
    let key = list[j],
      i = j - 1;
    while (i >= 0 && list[i] > key) {
      list[i + 1] = list[i];
      i = i - 1;
      await log(list);
    }
    list[i + 1] = key;
    await log(list);
  }
  return list;
}

async function log(msg) {
	let display = document.getElementById('display');
	display.innerHTML = msg.join(', ');
	/* consolg.log(msg) */;
	await sleep(800);
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

let ilist = [5, 2, 4, 6, 1, 3];
let olist = insertionSort(ilist);
/* console.log('InsertionSort', ilist, olist); */