HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Sorting array with at most two inversions

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
sortinginversionsarraywithtwomost

Problem

I have created an algorithm to sort an array of size $n$ with at most 2 inversions with exactly $n$ comparisons in the worst case.

I have no idea how to prove that it is optimal in terms of the number of comparisons. The only argument which came up to my mind is that we of course have to read the whole array, but I don't think it is a proper argument. I tried to use argument similar to minimal number of comparisons to find minimum (with graph), but it doesn't seem to be good in that case.

How can I prove that $n$ comparisons is optimal in the worst case?

Solution

To show that the optimal number of comparisons is $m$, you have to prove that any algorithm that uses $m-1$ comparisons must make a mistake on some arrays. The usual way to accomplish that is through an adversary argument – you "react" to the queries of the algorithm in a specific way, and ensure that after $m-1$ steps, there are two different linear orders that are consistent with your answers to the queries (as well as comply with the constraints – in this case at most two inversions).

Context

StackExchange Computer Science Q#54663, answer score: 2

Revisions (0)

No revisions yet.