0
$\begingroup$

Calculate the complexity of the algorithm as follows O (n ^ 2) Would it be correct?

int Smming(int n) {
      int i, j, sum = 0;
      for (i=n; i>0; i=i-1 {
          for (j=1; j<n; j*=2){
               sum+=i+2*j;
           }
       }
       return sum;
    }

And in the code below I calculated it to be O (n^2), would it be correct?

void B (int* vet, int n)
{
 int i, j, pos;
 for (i=0; i<n-1; i++)
 {
    pos = i;
    for (j=i+1; j < n; j++)
       if (vet[pos] > vet[j])
           pos = j;
       if (pos != i) {
           int tmp = vet[pos];
           vet[pos]=vet[i];
           vet[i] = tmp;
          }
   }
}
$\endgroup$
  • 3
    $\begingroup$ We discourage "please check whether my answer is correct" questions, as only "yes/no" answers are possible, which won't help you or future visitors. See here and here. Can you edit your post to ask about a specific conceptual issue you're uncertain about? As a rule of thumb, a good conceptual question should be useful even to someone who isn't looking at the problem you happen to be working on. If you just need someone to check your work, you might seek out a friend, classmate, or teacher. $\endgroup$ – D.W. Oct 9 '20 at 23:52
  • $\begingroup$ Most named polynomial algorithms are in $O(n²³)$ - find out whether lowest trivial upper bound is required or even a proof that the bound you give is tight. $\endgroup$ – greybeard Oct 10 '20 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.