Insertion sort loops over items in the array, inserting each new item into the s
ID: 3937288 • Letter: I
Question
Insertion sort loops over items in the array, inserting each new item into the subarray before the new item.
Once implemented, uncomment the Program.assertEqual() at the bottom to verify that the test assertion passes.
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) {
};
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]);
----------------------------------------------------------------------------
HINT:
Explanation / Answer
var insertionSort = function(array) {
for(var j = 1; j < array.length; j++){
insert(array,j-1,array[j]);
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.