javascript - Why my code doesn't run ? Insertion Sort -
function of insertion sort not working, error : a function taking long run. perhaps have mistake in code?
// function var insert = function(array, rightindex, value) { for(var j = rightindex; j >= 0 && array[j] > value; j--) { array[j + 1] = array[j]; } array[j + 1] = value; }; var insertionsort = function(array) { for(var = 0; < array.length ; i++) { insert(array,i,array[i+1]); } }; var array = [22, 11, 99, 88, 9, 7, 42]; insertionsort(array); println("array after sorting: " + array); program.assertequal(array, [7, 9, 11, 22, 42, 88, 99]);
var insert = function(array, rightindex, value) { for(var j = rightindex; j >= 0 && array[j] > value; j--) { array[j + 1] = array[j]; } array[j + 1] = value; }; var insertionsort = function(array) { for(var = 0; < array.length - 1 ; i++) { insert(array,i,array[i+1]); } }; var array = [22, 11, 99, 88, 9, 7, 42]; insertionsort(array); console.log(array);
you should run loop 1 less iteration calling insert() function + 1;
Comments
Post a Comment