Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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]);
}
};