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.
 
Thread 'Use greedy vertex coloring algorithm to prove the upper bound of χ'
Hi! I am struggling with the exercise I mentioned under "Homework statement". The exercise is about a specific "greedy vertex coloring algorithm". One definition (which matches what my book uses) can be found here: https://people.cs.uchicago.edu/~laci/HANDOUTS/greedycoloring.pdf Here is also a screenshot of the relevant parts of the linked PDF, i.e. the def. of the algorithm: Sadly I don't have much to show as far as a solution attempt goes, as I am stuck on how to proceed. I thought...
Back
Top