Created
March 1, 2021 15:06
-
-
Save bogoreh/0762a6897b3ce29409dca82dc9799dfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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