On strengthening the inequality between Osc and Runs
In the [previous blog post]1 we analyzed the relationships between the values returned by the measures of disorder \(\mathit{Osc}\), \(\mathit{Runs}\), \(\mathit{Mono}\) and \(\mathit{Reve}\). The goal was to provide a bound between the last two and \(\mathit{Osc}\), with \(\mathit{Runs}\) being first and foremost provided to in order reuse the method that led to its bound.
Today \(\mathit{Runs}\) is becoming the main focus of that intricate polycule, and as such will receive some of the care and attention it deserves.
I don’t want to repeat most of the previous article, so users are invited to refer to it for definitions.
The story so far…
Okay, I guess we’re still gonna repeat parts of it, aren’t we? You can skip this section if the information is still fresh in your mind from last time.
Levcopoulos and Petersson provide a first bound on the inequality between \(\mathit{Runs}\) and \(\mathit{Osc}\) in Heapsort—Adapted for Presorted Files, with the following logic:
Clearly, for all \(i\), \(\mathit{Cross}(x_i)\) cannot contain more than two elements from each run […]. Furthermore, the run to which \(x_i\) belongs cannot contribute with more than one element to \(\mathit{Cross}(x_i)\).
Where the aforementioned “two elements” from each run are respectively:
- The element preceding the point where \(y = x_i\) “crosses” the run.
- The last element of the run, if \(y = x_i\) crosses the line segment between the last element of the run and the first element of the next run.

These considerations led to the following bound:
\[\mathit{Osc}(X) = \sum_{i=1}^{\lvert X \rvert} \lvert \mathit{Cross}(x_i) \rvert \le \sum_{i=1}^{\lvert X \rvert} 2 \cdot (\mathit{Runs}(X) + 1) - 1\]That’s a pretty simple sum where:
- \(\lvert X \rvert\) is the number of \(\mathit{Cross}(x_i)\).
- \(\mathit{Runs}(X) + 1\) is the number of runs in \(X\)2.
- \(2\) is the maximal number of elements of a run that contribute to some \(\mathit{Cross}(x_i)\).
- \(-1\) accounts for the removal of one element of the run to which \(x_i\) belongs.
We already improved on that bound by showing that the last run of \(X\) can contribute at most one element to \(\mathit{Cross}(x_i)\). If \(x_i\) belongs in the last run of \(X\), then that run contributes zero element to \(\mathit{Cross}(x_i)\). That gave us a slightly better bound:
\[\mathit{Osc}(X) \le \sum_{i=1}^{\lvert X \rvert} 2 \cdot \mathit{Runs}(X)\]Guess what else never contributes more than two elements to any \(\mathit{Cross}(x_i)\)? Well…
Runs made of a single element
\(\mathit{Runs}(X)\) doesn’t tell us much about the size of the individual runs in \(X\), though we can still try to derive a lower bound to the number of single-element runs. It is trivial enough to see that:
- The smallest possible runs besides single-element ones are made of \(2\) elements.
- There can be at most \(\lfloor \frac{\lvert X \rvert}{2} \rfloor\) runs of \(2\) elements in any sequence \(X\).
Through grand feats of logic that never fails to remind me of the pigeonhole principle, we can already conclude that a sequence \(X\) for which \(\mathit{Runs}(X) + 1 > \frac{\lvert X \rvert}{2}\) contains at least \(\mathit{Runs}(X) + 1 - \frac{\lvert X \rvert}{2}\) single-element runs.
Let \(r_1\) and \(r_2\) respectively denote the numbers of runs of \(1\) and \(2\) elements into which \(X\) can be partitioned: For any \(x_i\) we have:
\[\mathit{Cross}(x_i) \le r_1 + 2 \cdot r_2 - 1\]Here we can’t remove two elements like we did in the previous section because a single-element run could be both the last element of \(X\), and the element preceding the point where \(y = x_i\) crosses the run. We can only remove \(1\) element from a single-element run.
Taking \(r_1 = \mathit{Runs}(X) + 1 - \frac{\lvert X \rvert}{2}\) and \(r_2 = \mathit{Runs}(X) + 1 - r_1\), we get the following graph3:

However that’s an unrealistically high bound: the values of \(r_1\) and \(r_2\) we picked above give us \(r_1 + r_2 > \mathit{Runs}(X) + 1\). To get the exact values of \(r_1\) and \(r_2\) for a given value of \(\mathit{Runs}(X)\), we need to solve the following system:
\[\begin{cases} r_1 + r_2 & = \mathit{Runs}(X) + 1\\ r_1 + 2 \cdot r_2 & = \lvert X \rvert \end{cases}\]…or do we?
We already know that \(\mathit{Cross}(x_i) \le r_1 + 2 \cdot r_2 - 1\) and that \(r_1 + 2 \cdot r_2 = \lvert X \rvert\), which gives a swift \(\mathit{Cross}(x_i) \le \lvert X \rvert - 1\) where \(\mathit{Runs}(X)\) disappears completely. In other words:
\[\mathit{Osc}(X) \le \sum_{i=1}^{\lvert X \rvert} \lvert X \rvert - 1 = \lvert X \rvert (\lvert X \rvert - 1)\]
Pretty compelling result, right?
Mmmh? That red dash-dotted line at the bottom of the graph?
…
In their paper Adaptive Heapsort — which I believe is just a refreshed and completed version of Heapsort—Adapted for Presorted Files—, Levcopoulos and Petersson prove that \(\mathit{Osc}(X) \le \frac{\lvert X \rvert(\lvert X \rvert - 2)}{2}\) for any sequence \(X\).
I suppose that it makes this whole article an exercise in futility.
Notes
-
\(\mathit{Runs}(X)\) counts the number of runs in \(X\), minus \(1\), to respect Mannila’s axioms for measures of presortedness. See Relationship between Amp and Runs for more information. ↩
- \[\begin{aligned} r_1 + 2 \cdot r_2 - 1 & = r_1 + 2 \cdot (\mathit{Runs}(X) + 1 - r_1)\\ & = r_1 + 2 \cdot \mathit{Runs}(X) + 2 - 2 \cdot r_1\\ & = 2 \cdot \mathit{Runs}(X) + 2 - r_1\\ & = 2 \cdot \mathit{Runs}(X) + 2 - (\mathit{Runs}(X) + 1 - \frac{\lvert X \rvert}{2})\\ & = 2 \cdot \mathit{Runs}(X) + 2 - \mathit{Runs}(X) - 1 + \frac{\lvert X \rvert}{2}\\ & = \mathit{Runs}(X) + 1 + \frac{\lvert X \rvert}{2} \end{aligned}\]