0

I am trying to calculate the primitive operations in a section of code written in Java. Here is what I have come up so far:

void bubbleSort(int array[])                Primitive Operations
    int n = array.length`;                  2 
    for (int i = 0; i &lt n - 1; i++)         3n – 1 = 1 + n + 2(n-1)
        for (int j = 0; j &lt n-i-1; j++)     ∑_(i=0)^(n-1)〖(3j-1)〗  
        {       
            if (array[j] > array[j+1])      ∑_(i=0)^(n-1) ∑_(j=0)^(n-1)〖(4)〗= ∑_(i=0)^(n-1) 4n
            { 
                 int temp = array[j];       ∑_(i=0)^(n-1) ∑_(j=0)^(n-1)〖(2)〗= ∑_(i=0)^(n-1) 2n
                 array[j] = array[j+1];     ∑_(i=0)^(n-1) ∑_(j=0)^(n-1)〖(3)〗= ∑_(i=0)^(n-1) 3n
                 array[j+1] = temp;         ∑_(i=0)^(n-1) ∑_(j=0)^(n-1)〖(3)〗= ∑_(i=0)^(n-1) 3n
            } 
     }
} 

One thing that I am uncertain is the inner loop of ∑_(i=0)^(n-1)〖(3j-1)〗 since there are two variables in the testing clause of the loop.

Thanks in advance for your time.

Paul

  • 1
    Already answered at https://stackoverflow.com/questions/26898148/counting-primitive-operations-calculating-big-o-notation, there is no consensus about primitive operations in java, if this is a class assignment, you will need to go with your instructor's suggestions. – Victor Polo De Gyves Montero Feb 05 '20 at 04:40
  • In a `for (A; B; C)` loop that iterates `N` times, `A` executes `1` time, `B` executes `N+1` times, and `C` executes `N` times, i.e. `A + B * (N + 1) + C * N` = `A + B + N * (B + C)`. Since your code has `A=1, B=2, C=1, N=n-1`, you get `1 + 2 + (n-1) * (2 + 1)` = `3n`, not the `3n – 1` you have. – Andreas Feb 05 '20 at 05:15

0 Answers0