Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Determine the "worst-case" runtime for the following sorting algorithm written in this code? 


1 sort (array a) {
2     for (n=a.size; n>1; --n) {
3         for (i=0; i<n-1; ++i) {
4             if (a[i] > a[i+1]) {
5                 a.swap(i, i+1)
6             }
7         }
8     }
9 }

Determine the worst-case runtime:


O(n2)

	
O(1)

	
O(n*log(n))

	
O(n)


What I have tried:

which option determine the worst-case runtime for the following sorting algorithm written in  this code?

O(n2)

	
O(1)

	
O(n*log(n))

	
O(n)
Posted
Updated 20-Dec-21 11:06am
Comments
Rick York 20-Dec-21 16:25pm    
One of the above. Do you really expect us to do your homework for you?

It is not difficult. Just follow the code trying to figurate the (rough) maximum number of swap operations needed in the worst scenario.
 
Share this answer
 
Comments
Greg Utas 21-Dec-21 13:58pm    
The time complexity of a sort algorithm is usually based on the number of comparisons.
CPallini 21-Dec-21 14:03pm    
In this case, the number of comparisons is independent of the worst case scenario.
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
So read your course notes on working out Big O notation, and try applying it. We aren't here to do that for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900