15,400,433 members

Copy Code

Determine the "worst-case" runtime for the following sorting algorithm written in this code?

Copy 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 }

Copy Code

Determine the worst-case runtime:

Copy Code

```
O(n2)
O(1)
O(n*log(n))
O(n)
```

Copy Code

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

Copy Code

```
O(n2)
O(1)
O(n*log(n))
O(n)
```

Comments

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.

Comments

The time complexity of a sort algorithm is usually based on the number of comparisons.

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!

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!

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