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
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started