Insertion Sort

Run Settings
LanguageJavaScript
Language Version
Run Command
function insertionSort(arr){ const length=arr.length; for(let i=0; i<length; i++){ if(arr[i]<arr[0]){ arr.unshift(arr.splice(i,1)[0]); } else{ if(arr[i]<arr[i-1]){ for(let j=1; j<i; j++){ if(arr[i]>=arr[j-1]&& arr[i]<arr[j]){ arr.splice(j,0, arr.splice(i,1)[0]); } } } } } return arr; } let array=[89,45,2,42, 76, 12, 48, 17, 95,1]; console.log(insertionSort(array));
Editor Settings
Theme
Key bindings
Full width
Lines