15,400,433 members
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
Rick York 20-Dec-21 16:25pm

One of the above. Do you really expect us to do your homework for you?

## Solution 1

It is not difficult. Just follow the code trying to figurate the (rough) maximum number of swap operations needed in the worst scenario.
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.

## Solution 2

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!