What is the Time Complexity of this Sorting Algorithm on an Array?

  • Thread starter Thread starter darkvalentine
  • Start date Start date
  • Tags Tags
    Complexity Time
darkvalentine
Messages
11
Reaction score
0

Homework Statement



Compute the time complexity of the following sorting algorithm on an array L[0..n-1] in terms of n. Basic Operation only includes comparison and swap.
sort (L, n)
{
int i=0, j;
while(i<n-1){
s = i ;
j=i+1;
while(j<n){
if (L[j]<L) s = j;
j++;
}
swap (L,L);
i++;
}
}


Homework Equations



N/A

The Attempt at a Solution


Well, we have just studied about the big O notation last week, but I have no idea how we apply it to solve this kind of problem.
 
Physics news on Phys.org
It looks to me like n^2 because of the nested loops.
 
╔(σ_σ)╝ said:
It looks to me like n^2 because of the nested loops.

Thank you, I think it x2 because of 2 loops, and each loop is executed n times.
 
Yes n^2. The inner loop has a worst case of n and the outter loop also runs n times.
 
There are two things I don't understand about this problem. First, when finding the nth root of a number, there should in theory be n solutions. However, the formula produces n+1 roots. Here is how. The first root is simply ##\left(r\right)^{\left(\frac{1}{n}\right)}##. Then you multiply this first root by n additional expressions given by the formula, as you go through k=0,1,...n-1. So you end up with n+1 roots, which cannot be correct. Let me illustrate what I mean. For this...
Back
Top