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.
 
Prove $$\int\limits_0^{\sqrt2/4}\frac{1}{\sqrt{x-x^2}}\arcsin\sqrt{\frac{(x-1)\left(x-1+x\sqrt{9-16x}\right)}{1-2x}} \, \mathrm dx = \frac{\pi^2}{8}.$$ Let $$I = \int\limits_0^{\sqrt 2 / 4}\frac{1}{\sqrt{x-x^2}}\arcsin\sqrt{\frac{(x-1)\left(x-1+x\sqrt{9-16x}\right)}{1-2x}} \, \mathrm dx. \tag{1}$$ The representation integral of ##\arcsin## is $$\arcsin u = \int\limits_{0}^{1} \frac{\mathrm dt}{\sqrt{1-t^2}}, \qquad 0 \leqslant u \leqslant 1.$$ Plugging identity above into ##(1)## with ##u...
Back
Top