Skip to content

Instantly share code, notes, and snippets.

@bogoreh
Created March 1, 2021 15:06
Show Gist options
  • Save bogoreh/0762a6897b3ce29409dca82dc9799dfa to your computer and use it in GitHub Desktop.
Save bogoreh/0762a6897b3ce29409dca82dc9799dfa to your computer and use it in GitHub Desktop.
var insert = function(array, rightIndex, value) {
var i;
for (i = rightIndex; i >= 0 && array[i] > value; i--){
array[i+1] = array[i];
}
array[i+1] = value;
};
var array = [3, 5, 7, 11, 13, 2, 9, 6];
insert(array, 4, 2);
println("Array after inserting 2: " + array);
Program.assertEqual(array, [2, 3, 5, 7, 11, 13, 9, 6]);
insert(array, 5, 9);
println("Array after inserting 9: " + array);
Program.assertEqual(array, [2, 3, 5, 7, 9, 11, 13, 6]);
insert(array, 6, 6);
println("Array after inserting 6: " + array);
Program.assertEqual(array, [2, 3, 5, 6, 7, 9, 11, 13]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment