0

So I was given this algorithm to compute the efficieny in which I found sorts an array into ascending order.

solvethis(T[1..n])
    for i = 1 to n-1 do   //n
       minJ = i           //n-1
       minX = T[i]        //n-1
       for j = i+1 to n do  //sum_{i=1}^{n} t
          if T[j] < minX then ** //sum_{i=1}^{n} t-1
             minJ = j             //sum_{i=1}^{n} t-1
             minX = T[j]         ////sum_{i=1}^{n} t-1
      T[minJ] = T[i]      //n-1
      T[i] = minX         //n-1

I also listed the number of times the line of code is run. I need to find the efficiency and how many time the line at ** is calculated. Any help is appreciated thank you.

John
  • 3
  • 1
  • 3

1 Answers1

0

The best way (The way I was taught at least) is to look for a basic operation. That's the operation that most significantly impacts the performance of an algorithm. In your case it looks like if T[j] < minX then would be your basic operation as it's performed the most times, where the statements inside of the if check are only performed if the condition is true. The fact you have two for loops going from 1 to n-1 tell me that you're working with quadratic time. You're going to be doing your basic operation at minimum n^2 or n operations n times. This should tell you that your algorithm performs in O(n^2) timing.

CS2016
  • 221
  • 1
  • 5