Is this function O(n) or O(log(n)) time complexity.
function reverse(array) {
for (var i = 0, j = array.length - 1; i < j; i++, j--) {
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
At first glance it appears to be making n/2 iterations over the input. However, if you think about it, the actual number of lower level operations is closer to 2n.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…