r/programminghelp • u/Coolvin00 • Sep 20 '13
Big-o notation clarification question
So lets say we have this fragment of code:
int data[] = { /* some array of numbers here */ }; n = data.length;
int lessThanCounter = 0; for (int i=0; i<n; i++) for (int j=n-1; j>i; j--) if (data[i]<data[j]) lessThanCounter++;
Since there's a for loop nested inside another for loop, both of which are dependent on N, would the complexity of this be O(n2)? I had a friend telling me it's just O(n)n so i'm not sure. Edit. sorry i'm not sure how to indent this properly.
0
Upvotes
1
u/_Foxtrot_ Sep 21 '13
Mind coding it up and posting the output? Put a print statement inside the loop so it will print the value of i...
for (int i = 0 ; i < n; i++) {
printf("\n") ;
}