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.