Algorithms - Challenge: implement insert
Hello good people,
I have some trouble with this challenge. I'm getting expected sorted array and correctly done second step but the first one is still not completed and i can't finnish this challenge. I probably didn't do challenge as it was expected but i'm not quite sure how can i do it more efficently since im just learning!
This is the hint im getting.
var = ?; for(? = ...; ... ; ...) { array[? + 1] = ...; } ...; ;
And this is how i did my code:
var insert = function(array, rightIndex, value) {
for(var i = rightIndex + 1; array[i] <= array[i-1]; i--){
var temp = array[i];
array[i] = array[i-1];
array[i-1] = temp;
}
};
Link to this challenge is:
https://www.khanacademy.org/computing/computer-science/algorithms/insertion-sort/pc/challenge-implement-insert
Any help would be really appreciated :)
Thanks!
Please sign in to leave a comment.