So for every insertion will cost O(n) any ways.

IF you array is already sorted you can insert new element into sorted array in O(n)…by moving comparing last nth element with the new value and moving it to n+1th position. ]]>

]]>function binaryInsert(x, array) {

var l = 0,

r = array.length – 1,

m;

while (l x) {

r = m – 1;

continue;

}

l = m + 1;

if (array[m] === x) {

break; // replace with return if no duplicates are desired

}

}

array.splice(l, 0, x);

}