1
$\begingroup$

Before I begin, I apologize for the bad wording. Consider the following "sorting algorithm":

Suppose there are $n$ books on the bookshelf labeled $1$-$n$, and ordered from left to right in a random way. Your goal is to end up with a sorted bookshelf from left to right with $1$ at the left and $n$ at the right. The algorithm is as follows:

  1. Pick a book at random that's not already in its correct position, say book $r$.
  2. Remove the book from its current position and place it at its correct position (position $r$)
  3. In the process, slide the books in between the original incorrect position of book $r$ and position $r$ towards the gap left open.
  4. Repeat if it's not sorted already, and if it is sorted, the algorithm terminates.

For example, suppose $n = 5$ and our original permutation is $5, 4, 3, 2, 1$ and suppose $r = 2$. Then after one step of the algorithm, the permutation becomes $5, 2, 4, 3, 1$.

Prove that regardless of the choice of the random book at each step of the algorithm that the algorithm will eventually terminate.

What is the worst case runtime of this algorithm? (I believe it occurs with $2, \dots, n, 1$ with $r$ being the left most book at each step and runtime roughly $2^{n - 2}$).

$\endgroup$
5
  • 3
    $\begingroup$ See M. Aigner, D. B. West "Sorting by insertion of leading elements". Journal of Combinatorial Theory Series A, 45(2), 1987, 306-309. doi.org/10.1016/0097-3165(87)90022-7 $\endgroup$ Commented Mar 1, 2022 at 2:10
  • 1
    $\begingroup$ @Max Alekseyev Thank you so much for the reference! But this only considers the case where the book chosen at each step is the leftmost book. $\endgroup$
    – James Leng
    Commented Mar 1, 2022 at 3:23
  • 1
    $\begingroup$ This problem was discussed in Russian paper "Задача о книжной полке" ("A problem about a bookshelf") - see pages 254-256 in mccme.ru/free-books/matpros/pdf/mp-19.pdf $\endgroup$ Commented Mar 1, 2022 at 3:53
  • $\begingroup$ A graph of pessimal selections for $n=6$ gives some hints for how to show that $(2, \ldots, n, 1)$ takes $2^{n-1} - 1$ steps. Note in particular that after the pinch point at the half-way mark it can be reduced to $(2, \ldots, n-1, 1, n)$. $\endgroup$ Commented Mar 1, 2022 at 15:06
  • 1
    $\begingroup$ @PeterTaylor: For $(2,\dots,n,1)$, attainability of $2^{n-1}-1$ is proved by induction in the paper "A problem about a bookshelf" cited above. For a general permutation, they give an upper bound $2^n - 2^{n/2}$. $\endgroup$ Commented Mar 1, 2022 at 17:51

1 Answer 1

4
$\begingroup$

Here is a quote of my 2009 proof @ AoPS that the algorithm will eventually terminate:

For a permutation $ p$, consider a function $ f(p) := \sum_{j=1}^n |p_j - j|$, i.e., the sum of absolute differences of elements and their positions in $ p$. In the sorting process this function never increases but occasionally can decrease. It is clear that $ f(p)\geq 0$ and $ f(p) = 0$ only if the permutation $ p$ is sorted. In an infinite sorting this function must take a constant non-zero value after some step.

Suppose that there exists an infinite sorting process and let $ n$ be the smallest possible integer that admits such an infinite sorting.

Let $ x$ be the smallest elements that jumps infinite number of times. Then after some step in the sorting process no element smaller than $ x$ jumps at all and the function $ f(p)$ does not decrease. Without loss of generality assume that this happens straight from the beginning of the sorting process (simply by ignoring everything what happened before).

Since $ x$ jumps infinite number of times, it takes its correct place in the permutation an infinite number of times. However, it can leave its correct place only if there exists an element larger than $ x$ somewhere before $ x$ (whose jump would shift $ x$ from its correct place). Therefore, there exists an element $ y$ smaller than $ x$ somewhere after $ x$.

Since $ y < x$, it never jumps itself and can be shifted by jumps of other elements only towards the end of the permutation (as otherwise $ f(p)$ would decrease). But this way it cannot be shifted infinitely many times, so after some point $ y$ takes a fixed place in the permutation. As soon as that happened, the sorting can be considered as two independent sortings -- at the left hand side of $ y$ and at the right hand side of $ y$ since no element jumps across $ y$ from one "half" to the other. Moreover, sorting of the left "half" must be infinite as it contains $ x$. Renaming the elements at the left hand side of $ y$ will give a permutation on smaller number of elements that admits an infinite sorting, a contradiction to the minimality of $ n$. This contradiction proves that no infinite sorting is possible.


Another approach:

Alternatively, we can take $ y = 1$ right away (without considering any $x$) and notice that if it ever takes its correct place then we have an infinite sortings of the remaining $n-1$ elements, a contradiction to the minimality of $ n$. Otherwise, $ y$ will be shifted towards the end of the permutation and sooner or later will take a fixed position, again splitting the permutation into two smaller ones, at least one of which will be subject to infinite sorting in contradiction to the minimality of $n$.


PS. It is conjectured that $2^{n-1}-1$ is the tight upper bound for the number of steps (attained at permutation $(n,1,2,\dots,n-1)$, for example), but to best of my knowledge it was not proved.

$\endgroup$
1
  • $\begingroup$ Thanks for the solution! I have seen multiple solutions to this problem before years ago, but I don't remember any of the ones I've seen before. One solution particularly caught my eye: it used a strict monovariant. As you observed in your solution, there is an "obvious" weak monovariant (which you call $f$), but it turns out there is a not so obvious strict monovariant. Not to discount your solution to this problem, I posted this hoping that someone would remind me what that strict monovariant was. $\endgroup$
    – James Leng
    Commented Mar 2, 2022 at 0:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .