Question: ///Please look at this js code function insertionSort part isn't working if you run it you'll see. Thank thank you function genRandomArray(n) { var arr

///Please look at this js code function insertionSort part isn't working if you run it you'll see. Thank thank you

function genRandomArray(n) { var arr = []; for (var i = 0; i < n; i++) { arr[i] = Math.round(10 * Math.random()); } return arr; }

// This implements the swap function function swap(array, index1, index2) { var x = array[index2]; array[index2] = array[index1]; array[index1] = x; return array; }

// This implements the shift function function shift(array, index1, index2) { if (index1 < index2) { return array; } var x = array[index1]; for (var i = index1; i >= index2 + 1; i--) { array[i] = array[i - 1]; } array[index2] = x; return array; }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// The code below isn't working it's not sorting into an order. !!!!

function insertionSort(array,index1,index2){ var n = array.length; for (var index1 = 2; index1 < n; index1++){ index1 = index2;

while(array[index1] < array[index2 - 1] > array[index2 > 1]){ index2 = index2 - 1; } shift(array,index1,index2); arr = array; } return array; }

// This will generate a random array with 12 elements, print it to the console, and also //print what is returned by insertionSort also to the console var arr = genRandomArray(12); console.log(arr) console.log(insertionSort(arr));

// Do not modify the code below this point-------------------------------- module.exports = { genRandomArray: genRandomArray, swap: swap, shift: shift, insertionSort: insertionSort }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!