I have this implementation of reverse
algorithm:
function reverse(a) {
let left = 0;
let right = a.length - 1;
while (left < right) {
let tmp = a[left];
a[left] = a[right];
a[right] = tmp;
left += 1;
right -= 1;
}
return a;
}
For any array of n
elements the loop will run n/2
times. But I read everywhere that reverse
algorithm complexity is O(n)
? So why is the complexity O(n)
? Because of two operations on each cycle?