What exactly constitutes the worst case input of various sorting algorithms?

295 Views Asked by At

This is related to the this question.

I naively used to think that the worst case of all sorting algorithms was an input given in descending order. But it seems that I was wrong.

For example according to this post, the worst case of merge sort looks quite different than just an array with numbers in descending order.

So my question is that what exactly constitutes a worst case scenario of an algorithm. Can this be formally represented ? And is there a way/technique of deriving/proving this from the algorithm?

1

There are 1 best solutions below

5
On

The formal definition of a worst case scenario is that it's an input for which for a given size the algorithm takes the maximum number of steps. There may be more than one such input.

As you discovered, the set of worst case scenarios depends on the algorithm. There's no generic way to find one without knowing the algorithm.