function insertSort(arr){ var len = arr.length for(var i=1;i<len;i++){ var temp = arr[i] var j = i-1 while(arr[j]>temp){ arr[j+1]=arr[j] j-- } arr[j+1]=temp } return arr } var arr = [5,3,2,7,8] console.log(insertSort(arr)) //[2,3,5,7,8]
function insertSort(arr){ var len = arr.length for(var i=1;i<len;i++){ var temp = arr[i] var j = i-1 while(arr[j]>temp){ arr[j+1]=arr[j] j-- } arr[j+1]=temp } return arr } var arr = [5,3,2,7,8] console.log(insertSort(arr)) //[2,3,5,7,8]