function insertionSort(insArray, compare = function(a,b){ return a < b; } ){
if(insArray.length < 2 ){
return insArray;
}
//element to compare
for( let i = 1; i < insArray.length; i++){
//compare element to all preceding elements
/** delete this line **/ document.getElementById("output").innerHTML += "Comparing: " + insArray[i] + "
";
for( let j = 0; j < i; j++ ){
//is current element smaller than a preceding element
if( compare(insArray[i], insArray[j]) ){
/** delete this line **/ document.getElementById("output").innerHTML += " " + insArray[i] + " is smaller than " + insArray[j] + "
";
let temp = insArray.splice(i,1); //remove the smaller element
insArray.splice(j,0,temp[0]); //insert smaller element at preceding element's location
break;
}
}
/** delete this line **/ document.getElementById("output").innerHTML += " " + insArray + "
";
}
return insArray;
}