4

Source code:

function CreateArray(length) {
    var array1 = [];
    for (var k = 0, t = length; k < t; k++) {
        array1.push(Math.round(Math.random() * 3000000))
    };
    return array1;
var array = CreateArray(100,500,1000) // works only for 100


console.time("insertionSort")

function insertionSort(array) {
    var countOuter = 0;
    var countInner = 0;
    var countSwap = 0;

    for(var i = 0; i < array.length; i++) {
        countOuter++;
        var temp = array[i];
        var j = i - 1;
        while (j >= 0 && array[j] > temp) {
            countInner++;
            countSwap++;
            array[j + 1] = array[j];
            j--;
        }
        array[j + 1] = temp;
    }

    console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
    return array;
}
console.timeEnd("insertionSort")

insertionSort(array.slice()); 

with this last calling I want to check 100,500,1000,5000 and other different lengths. Any ideas?

I want that the last calling will work for any lengths of arrays.

1 Answer 1

3

You need CreateArray to create multiple arrays at once, and then you need insertionSort to be able to process multiple arrays at once - or, even better, call another function (once) that calls insertionSort for each array:

const CreateArray = (...lengths) => lengths.map(length => (
  Array.from({ length }, () => Math.floor(Math.random() * 3000000))
));

function insertionSort(array) {
  var countOuter = 0;
  var countInner = 0;
  var countSwap = 0;
  for (var i = 0; i < array.length; i++) {
    countOuter++;
    var temp = array[i];
    var j = i - 1;
    while (j >= 0 && array[j] > temp) {
      countInner++;
      countSwap++;
      array[j + 1] = array[j];
      j--;
    }
    array[j + 1] = temp;
  }
  console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
}
const insertionSortMultipleArrays = (arrs) => {
  arrs.forEach(arr => {
    console.time("insertionSort");
    insertionSort(arr);
    console.timeEnd("insertionSort");
  });
};
  
const arrays = CreateArray(100,500,1000,5000);
insertionSortMultipleArrays(arrays);

Sign up to request clarification or add additional context in comments.

4 Comments

nice solution :-)
@rollback Being pretty functional, It looks very readable to me (except for the insertionSort part, which is nearly by definition a little bit convoluted), what were you thinking of?
ok, i understood a little bit, but what about already sorted array: function Sorted(a, b) { if (a > b) return 1; if (a < b) return -1; } var array8 = []; for (var k= 0, t=100; k<t; k++){ array8.push(Math.round(Math.random() * 3000000)) }; array8.sort(Sorted); how to add this array with the any lenght(100,500,1000) to this algorithm?
Same way insertionSort was turned into insertionSortMultipleArrays - have another function call it multiple times (or turn your sort function into one that iterates over and sorts multiple arrays, rather than just a single one)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.