Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run time complexity for Reversal of array? [duplicate]

Why the run time complexity for this logic is O(N)? The number of iterations are only half here. Please explain!

for(int i = 0; i < validData.length / 2; i++)
{
    int temp = validData[i];
    validData[i] = validData[validData.length - i - 1];
    validData[validData.length - i - 1] = temp;
}
like image 660
chetan chouhan Avatar asked Feb 04 '23 19:02

chetan chouhan


1 Answers

Big O notation is about order of magnitudes and how the complexity relates to the number of elements. O(1/2 * n) == O(n)

like image 73
Andrew Jenkins Avatar answered Feb 07 '23 18:02

Andrew Jenkins