3

so I need to prove the following:

Prove that $n-1$ comparisons are sometimes necessary to test whether an array with $n$ distinct elements is sorted in increasing order, for any $n \geq 1$.

The problem comes with the following hint:

Assume an algorithm exists that always correctly tests if the array is sorted using at most $n-2$ comparisons and show there must exist an input where this algorithm fails.

Base on the hint, I tried proving this via contradiction and here is my attempt:

Assume an algorithm exists that correctly tests if the array is sorted using at most $n-2$ comparisons. Then, let A be an array of 2 random numbers $a$ and $b$. Given that we don't know the value of $a$ with relation to $b$ or vice versa, in order to find this relation, an thus find out if the array with n-distinct elements is sorted in increasing order, 1 comparison operation, namely between $a$ and $b$, is needed, but then $n-2 = 1-2 \neq 1$. Therefore we have arrived at a contradiction.

While this attempt is most likely wrong, I am somewhat familiar with proofs by contradiction, but I don't see how deriving a contradiction from my initial assumption (the one from the hint) helps prove that $n-1$ comparisons are necessary.

P. Jhon
  • 31
  • 1
  • 4

2 Answers2

3

You can use a simple adversary argument (in Jeff's lecture note) to prove that every correct algorithm has to compare $a_i$ with $a_{i+1}$ for $i=0,1, \ldots, n−2$.

If it is not the case, you can carefully choose the values such that $a_{i+1} < a_{i}$, without violating any other comparison results.

You are encouraged to fill the details.

hengxin
  • 9,671
  • 3
  • 37
  • 75
0

For any $n \geq 1$, $n-1$ comparisons are sometimes necessary to test whether an array with n distinct elements is sorted in increasing order. An adversary argument will be used to prove this.

Suppose we have an array $A$ consisting of elements $a_i, a_{i+1}...a_n$ with $a_i = i$ for $i=1$ to $n$.

Assume an algorithm exists that correctly tests if the array is sorted using at most $n-2$ comparisons, and returns true if the algorithm is sorted in increasing order and false if it is not. Now suppose that the algorithm is run on array A. Then since $n-2$ comparisons are performed and the array contains $n-1$ pairs of consecutive elements, there must be a pair of consecutive elements which has not been compared to one another. Note that since $a_i = i$ for every element of the array, the array is indeed sorted in increasing order and the algorithm will return true.

Following, suppose we find element $a_i$ which was not compared to its successor by the algorithm and change its value to $i+2$ (previously $i$), and run the algorithm on array A. Then A is not sorted in increasing order anymore,since $a_i = i+2 > a_{i+1} = i + 1$. but, since $a_i$ is never compared to $a_{i+1}$ the algorithm will still return true, which is incorrect.

P. Jhon
  • 31
  • 1
  • 4