Basic Complexity Analysis

Given two arrays A A and B B , both of size n 1 n \geq 1 , which of these three algorithms runs the slowest?

1
2
3
4
5
Algorithm algo1(A,B)
   b ← A[0]
   for i ← 1 to n -1 do
      b ←  b + A[i]
   return b

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Algorithm  algo2(A,B)
   c ←  0
   for i ←  1 to n -1 do
     s ←  0
     for j ←  1 to n -1 do
        s ←  s + A[0]
        for k ←  1 to n -1  do
           if k % 1 = 0 then
              return s                     
           s ←  s + 9

   return c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Algorithm  algo3(A,B)
   c ←  0
   for i ←  1 to n -1 do
     s ←  0
     for j ←  1 to n -1 do
        s ←  s + A[0]
        for k ←  1 to j do
           s ←  s + A[k]
        if B[i] = s then
           c ←  c + 1
   return c

Algo3 and Algo2 both run the slowest Algo3 Algo1 Algo2

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

0 solutions

No explanations have been posted yet. Check back later!

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...