Click to See Complete Forum and Search --> : A question - Data Structures
proff.hacker
Oct 10th, 2002, 02:54 PM
I want to Find the number of operations required to evaluate the following program segment and therefore to find the complexity with respect to Big-O notation
int result = 0;
for (int i = 1; i<n; i++)
for (int j=1; j<n; j++)
result += i*j
return result;
any help plz :confused:
HairyDave
Oct 10th, 2002, 03:07 PM
Well, if I remember by Big-O
int result = 0; // 1 operation - O(1)
for (int i = 1; i<n; i++) // loops through n times O(n)
for (int j=1; j<n; j++) // loops through n times O(n)
result += i*j // 1 operation - O(1)
return result;
I think this works out to be Big-O of n squared. The constants dont count if I remember correctly because you are looking at worst case - would be O(n*n*1+1) -> O(n*n).
HD
billrogers
Oct 10th, 2002, 04:18 PM
O(n^2)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.