Results 1 to 3 of 3

Thread: A question - Data Structures

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277

    Question A question - Data Structures

    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

    Code:
    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

  2. #2
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Well, if I remember by Big-O

    VB Code:
    1. int result = 0;                  // 1 operation - O(1)
    2. for (int i = 1; i<n; i++)     // loops through n times O(n)
    3.   for (int j=1; j<n; j++)    // loops through n times O(n)
    4.     result += i*j                // 1 operation - O(1)
    5. 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

  3. #3
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    O(n^2)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width