Insertion sort loops over items in the array inserting each

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:

Solution

var insertionSort = function(array) {
for(var j = 1; j < array.length; j++){
insert(array,j-1,array[j]);
}
};

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site