Sorting Permutations with Fixed Pinnacle Set

We give a positive answer to a question raised by Davis et al. ({\em Discrete Mathematics} 341, 2018), concerning permutations with the same pinnacle set. Given $\pi\in S_n$, a {\em pinnacle} of $\pi$ is an element $\pi_i$ ($i\neq 1,n$) such that $\pi_{i-1}<\pi_i>\pi_{i+1}$. The question is: given $\pi,\pi'\in S_n$ with the same pinnacle set $S$, is there a sequence of operations that transforms $\pi$ into $\pi'$ such that all the intermediate permutations have pinnacle set $S$? We introduce {\em balanced reversals}, defined as reversals that do not modify the pinnacle set of the permutation to which they are applied. Then we show that $\pi$ may be sorted by balanced reversals (i.e. transformed into a standard permutation $\Id_S$), implying that $\pi$ may be transformed into $\pi'$ using at most $4n-2\min\{p,3\}$ balanced reversals, where $p=|S|\geq 1$. In case $p=0$, at most $2n-1$ balanced reversals are needed.

Many studies have been devoted to the combinatorics of peaks, especially to enumeration and counting problems [1,2,3,6,9,11] (and many others). They identify strong and elegant relationships between peaks or descents in permutations, on the one hand, and Fibonacci numbers, Eulerian numbers, chains in Eulerian posets, etc. on the other hand.
In [5], Davis et al. revive the point of view considered in [4], and propose to identify peaks by their values rather than by their positions. They call a pinnacle any element π i with i = 1, n such that π i−1 < π i > π i+1 , and show that considering pinnacles instead of peaks changes the combinatorial considerations behind counting and enumerating permutations with a given peak/pinnacle set. They characterize the sets of integers that may be the pinnacle set of a permutation (so-called admissible pinnacle sets), count them, and propose bounds, involving the Stirling number, on the numbers of permutations with given pinnacle set.
They further ask several questions, one of which is considered in this paper: Question 4.2 in [5]. For a given admissible pinnacle set S, is there a class of operations that one may apply to any π ∈ S n whose pinnacle set is S to obtain any other permutation π ∈ S n with the same pinnacle set, and no other permutation? This question is motivated by the search for similarities between pinnacles and peaks [5]. In this paper we give a positive answer to this question. More particularly, we identify a reduced set of reversals (the operation that reverses a block of a permutation) -called balanced reversals -which do not modify the set of pinnacles. Then we show that it is possible to transform any permutation with pinnacle set S into a canonical permutation of the same size with pinnacle set S by applying a sequence of at most 2n − min{p, 3} balanced reversals. As the inverse transformation is always possible, this answers Question 4.2. above.
The paper is organized as follows. Section 2 contains the main definitions and notations. In Section 3 we identify balanced reversals and state the main results. Section 4 is devoted to the proof of our main theorem. This proof describes the algorithm allowing us to find the sequence of reversals transforming a given permutation into the canonical permutation. For the sake of completeness, we give in Section 5 the implementation details for an optimal running time of our algorithm. Section 6 is the conclusion.

Definitions and notations
Permutations π, π , π we use in the paper belong to the symmetric group S n , for a given integer n. Elements n + 1 and n + 2 are artificially added at the beginning and respectively the end of each permutation, so that a permutation π ∈ S n is written as π = (π 0 π 1 π 2 . . . π n π n+1 ) with π 0 = n + 1 and π n+1 = n + 2. For each i > 0, we define Prec π (π i ) = π i−1 and for each i < n + 1 we define Next π (π i ) = π i+1 . The block of π with endpoints π a and π b (where a ≤ b) is defined as (π a π a+1 . . . π b−1 π b ).
A pinnacle is any element π i with i = 0, n + 1 such that π i−1 < π i > π i+1 . Similarly to pinnacles (whose indices are the peaks), we define the dells (whose indices are the valleys). A dell of π is any element π i with i = 0, n + 1 such that π i−1 > π i < π i+1 . The shape of the permutation π is the permutation B π = (y 0 v 1 y 1 v 2 y 2 . . . , y p v p+1 y p+1 ) where v 1 , . . . , v p+1 are the dells of π, y 1 , . . . , y p are its pinnacles, whereas y 0 = π 0 = n + 1 and y p+1 = π n+1 = n + 2. The presence of the elements n + 1 and n + 2 at the beginning and the end of the permutation adds no pinnacle to the initial permutation, and ensures that dells v 1 , v p+1 exist.
Balanced reversals are characterized in the next section. In order to identify appropriate balanced reversals when needed, we make use of cutpoints. Let i be an integer with 1 ≤ i ≤ p + 1 and z be an element of π not belonging to A π (v i , y i ), such that v i < z < y i . The largest element e of A π (v i , y i ) ∪ {v i } such that e < z is called the cutpoint of z on A π (v i , y i ) and is denoted cutA π (z, v i , y i ). The similar definition holds for D π (y i , v i+1 ). Let i be an integer with 0 ≤ i ≤ p and z be an element of π not belonging to D π (y i , v i+1 ), such that v i+1 < z < y i . The largest element e of D π (y i , v i+1 )∪{v i+1 } such that e < z is called the cutpoint of z on D π (y i , v i+1 ) and is denoted cutD π (z, y i , v i+1 ).
Finally, define the following problem: BALANCED SORTING PROBLEM Input: A permutation π ∈ S n with pinnacle set S. Question: Is it possible to transform π into Id S ∈ S n using only balanced reversals?
The difficulty in solving this problem has mainly two origins: first, one cannot perform any wished reversal since a reversal is not necessarily balanced (see Example 2); and second, given a set S of pinnacles and a permutation σ of the elements in S, it is possible that no permutation π of given size n and with pinnacle S exists that has the pinnacles in the order (from left to right) given by σ.  5 7) we may find the permutation π = (8 2 3 1 5 4 7 6 9), but with n = 7 and σ = (3 7 5) there is no permutation from S n with pinnacles in this order. Therefore, the BALANCED SORTING PROBLEM is a question of feasibility in the first place. The optimal sorting is proposed as an open problem in the conclusion.

Main results
Let π ∈ S n be a permutation with pinnacle set S such that |S| = p. The main result of the paper is the following one. Theorem 1. There is a sequence R that solves the BALANCED SORTING PROBLEM on π using at most 2n − min{p, 3} balanced reversals when p ≥ 1, and at most 2n − 1 reversals when p = 0.
An answer to Question 4.2 is an immediate consequence of this theorem. Corollary 1. Let π, π ∈ S n be two permutations with pinnacle set S such that |S| = p. Then, when p ≥ 1 there is a sequence T of at most 4n − 2 min{p, 3} balanced reversals that transforms π into π , using only intermediate permutations with pinnacle set S. When p = 0, T contains at most 4n − 2 balanced reversals.
Proof. Let R be the sequence of balanced reversals needed to sort π according to Theorem 1. Similarly, let R = (ρ(w 1 , w 1 ), ρ(w 2 , w 2 ), . . . , ρ(w q , w q )) be the sequence of balanced reversals needed to sort π . Let T be the sequence made of R followed by the sequence ρ(w q , w q ), ρ(w q−1 , w q−1 ), . . . , ρ(w 1 , w 1 ). Then T transforms π into Id S and subsequently Id S into π using only balanced reversals. The definition of a balanced reversal guarantees that all the intermediate permutations have pinnacle set S.
Recall that, by Definition 1, in a reversal ρ(w 1 , w 2 ) the endpoints w 1 and w 2 are in this order from left to right on π and are distinct from y 0 , y p+1 . Depending on the position of w 1 and w 2 in π, balanced reversals are of different types and imply different constraints, that need to be satisfied in order to Type Positions of w1, w2 if Nextπ(w2) = yj+1 = yp+1 then w1 < Nextπ(w2) C.3 w1 = yi, w2 = yj, i < j w1 > Nextπ(w2) and w2 > Precπ(w1) Table 1: Different types of balanced reversals. Each reversal is defined by constraints on w 1 and w 2 , defining their places (middle column) and the relative orders required between some elements (rightmost column). Recall that w 1 and w 2 are in this order from left to right on π and are distinct from y 0 , y p+1 . Then Prec π (w 1 ) and Next π (w 2 ) always exist.
guarantee that the reversal is balanced. Table 1 presents the different possible positions for w 1 and w 2 , each defining a type. On the rightmost column are given the constraints that w 1 , w 2 and their adjacent elements must fulfill in order to obtain a balanced reversal. For instance, reversal ρ(w 1 , w 2 ) of type A.1 is obtained when w 1 belongs to an ascending set of π and w 2 belongs to a descending set of π.
One further requires that the following constraints be verified: when Next π (w 2 ) = v j+1 we must have w 1 > Next π (w 2 ); when Prec π (w 1 ) = v i we must have w 2 > Prec π (w 1 ). The standard cases A.1 and B.1 are shown in Figure 1. The other cases are obtained from A.1 or B.1 when w 1 or w 2 or both of them are a pinnacle or a dell. Cases denoted A.x are obtained from case A.1 only, cases B.x are obtained from B.1 only and cases C.x are obtained from both A.1 and B.1. Symmetrical cases are identified by an "s". We show below that these types form altogether the entire collection of balanced reversals. Proposition 1. Reversal ρ(w 1 , w 2 ) is balanced iff it belongs to the collection of types in Table 1.
If both w 1 and w 2 belong to ascending sets of the permutation, that is w 1 ∈ A π (v i , y i ) and w 2 ∈ A π (v j , y j ) with i ≤ j, then when the reversal is performed w 2 or Prec π (w 1 ) becomes a new pinnacle, a contradiction. A similar reasoning holds when both w 1 and w 2 belong to descending sets of the permutation. So these cases cannot appear.
If one element among w 1 and w 2 belongs to an ascending set of the permutation, and the other one to a descending set of it, then it is easy to check that only the conditions in type A.1. or in type B.1. guarantee that no new pinnacle is added.
If exactly one element among w 1 and w 2 is a dell, then we necessarily have one of the types A.3, B.3, C.1 (or the symmetric ones) since any other condition creates a new pinnacle or removes an and descending regions according to their values (high or low). A consequence is that neighboring elements on the permutation are not always at equal distance on the horizontal axis. Elements w 1 and w 2 are drawn as grey circles, Prec π (w 1 ) is drawn as a white square and Next π (w 2 ) is drawn as a black square. a) Permutation π. b) Result once the reversal ρ(w 1 , w 2 ) is applied. existing one.
If exactly one element among w 1 and w 2 is a pinnacle, then the other one is either a dell or belongs to an ascending or descending set of π. The former possibility necessarily leads to type C.1 (or the symmetric one). The latter possibility results into types A.2, A.2s, B.2 or B.2s.
If both w 1 and w 2 are pinnacles, or both are dells, then we must have the conditions in types C.2 or C.3 to preserve the pinnacle set.
⇐ : This part only requires to check, for each type, that the pinnacle set is not modified under the indicated conditions.

Proof of Theorem 1
We assume below that p ≥ 1 and postpone the case p = 0 to Remark 2, at the end of the section.
In order to build the sequence R required in Theorem 1, we follow three steps: 1. Sort the p pinnacles of π in increasing order.
2. Place the wished dells (i.e. the dells of Id S ) as dells of π, in increasing order.
3. Move each element belonging to an ascending or descending set of π on the rightside of the last dell of π, in increasing order.
The result of these three steps is Id S . Then R is the sequence of all the balanced reversals performed during these three steps in order to transform π into Id S . Remark 1. Note that in the subsequent, when permutation π is successively transformed using balanced reversals into some other permutation π , the elements of π are identified both by their names in π , i.e. π 1 , π 2 etc. and by their names in π, according to the needs. Once a given task is fulfilled by applying one or several balanced reversals, the resulting permutation is renamed as π, so that the following task begins with an initial permutation still denoted π.

Step 1: Sort the pinnacles
This is done by successively replacing the pinnacle y k , for k = 1, 2, . . . , p − 1, by the k-th lowest pinnacle without modifying the set of pinnacles. Then in the resulting permutation the highest pinnacle is necessarily y p . The other elements are not constrained at this step. Algorithm 1 presents the balanced rotations to be performed, as identified in this subsection.

Lemma 1.
There is a sequence of at most p − 1 balanced reversals that transforms π with pinnacle S into π * with pinnacle S such that y * 1 is the lowest pinnacle in π * . Moreover, when p ≥ 3, exactly one of the two following configurations occurs: (X) the sequence contains exactly p − 1 balanced reversals and y * p is the highest pinnacle in S.
(Y) the sequence contains at most p − 2 balanced reversals.
Proof. If y 1 is already the lowest pinnacle, then nothing is done. Assume now the lowest pinnacle is y i with i = 1. Several cases are possible, that we present below, before giving the algorithm.
Case 1). y i < v 1 and for all h > i, we have y h < v 1 .
Then v p+1 < y p < v 1 and the balanced reversal (type C.2) ρ(v 1 , v p+1 ) allows to obtain a permutation π with v 1 = v p+1 < v 1 . In π : a) if y i = y 1 , then we are done. b) if y i = y 1 and y i < v 1 , then we have that y p+1 = y 1 > v 1 > v 1 and we deduce that at least one pinnacle placed on the rightside of y i is larger than v 1 . Then π satisfies Case 2 below.
c) if y i = y 1 and y i > v 1 , then π satisfies Case 3 below.
Then we assume w.l.o.g. that h is the minimum index with this property.
is not a pinnacle and even if it may happen that and is smaller than v 1 as proved above. Let us call π this new permutation, whose elements satisfy y t = y i for some t, y h−1 = y 1 , y h = y h , y 1 = y h−1 . In π : a) if t = 1, then we are done.

Algorithm 1 Permutation sorting by balanced reversals : Step 1
Input: A permutation π ∈ S n with pinnacle set S of cardinality p. Output: The permutation π whose pinnacles have been placed in increasing order (Proposition 2) 1: x ← min{y 1 , . . . , y p } // x is the lowest pinnacle 2: let // h = 0 occurs when the first set is empty 5: if h = 0 then 6: end if 8: end if 9: while x is not the leftmost pinnacle of π do 10: let end if 17: end while // the lowest pinnacle is now placed in position y 1 18: for k = 1 to p − 2 do 19: x ← min{y k+1 , . . . , y p }; let y t = x // x is the lowest remaining pinnacle 20: end if 23: end for 24: Return π b) if t = 1 and y i < v 1 , then π satisfies Case 2 since v 1 = v h < v 1 < y 1 = y h−1 , so there is at least one index as required in Case 2. The smaller such index, say g, satisfies t < g < h.
c) if t = 1 and y i > v 1 , then π is in Case 3 below.
Condition t < g < h in item b means that the recursivity we find here will end, as we show later (once Case 3 is presented).
Let e = cutA π (y i , v 1 , y 1 ). Then ρ(Next π (e), y i ) is a balanced reversal (type A.2s if Next π (e) = y 1 and type C.3 otherwise) since y i > e and Next π (e) > y i > Next π (y i ), both by the definition of the cutpoint e. This reversal places y i as the leftmost pinnacle.
The algorithm consists in applying Case 1 if necessary, then Case 2 as long as the current permutation requires it (in item b of Case 2) and finally Case 3 if needed. It is presented in Algorithm 1. In order to compute the number of balanced reversals performed in the worst case, we denote: • π the initial permutation • π 0 the permutation obtained at the end of Case 1, whether it is applied or not (so that π 0 = π if not).
• π m+1 the permutation obtained once Case 3 is applied, if it is applied.
As a consequence, if m > 0 then for 0 ≤ q ≤ m − 1, permutation π q is transformed into permutation π q+1 using the balanced reversal ρ(v q 1 , v q h q ) of type C.2, as mentioned in Case 2 before. Here, h q denotes the minimum index h computed in Case 2 for each π q , i.e. h 0 = h (see Case 2), h 1 = g (see item b in Case 2), and so on. Indices h 0 , h 1 . . . , h m−1 computed respectively in π 0 , π 1 . . . , π m−1 satisfy (see again Case 2 item b where we show that g < h): . . , y q h 1 , y q h 0 are pinnacles in the permutation π q for each q with 0 ≤ q ≤ m − 1, in this order from left to right. Moreover, each permutation π q inherits the pinnacles of the previous permutation π q−1 , that is, y q h s = y q−1 h s for all 0 ≤ s ≤ q − 1 (meaning that the pinnacles as well as their indices in the permutation are the same).
(iii) h q and v q 1 satisfy the conditions of Case 2 item b in π q for each q with 0 ≤ q ≤ m − 1. , v m−1 h m−1 ), places y i as the leftmost pinnacle, and thus we are done. Item a in Case 2 applies, and no other reversal is needed. Then the total number of reversals applied in Step 1 is p − 1 when Case 1 applies and p − 2 otherwise. In the latter case, configuration (Y) in the lemma occurs. The former case is fixed using property (P) below. , v m−1 h m−1 ), places y i as the leftmost pinnacle, and thus we are done. Item a in Case 2 applies, and no other reversal is needed. Then the total number of reversals in step 1 is p − 2 when Case 1 applies and p − 3 otherwise, yielding configuration (Y) again.
-When m = p − 3 and h 0 = p, the total number of reversals applied in Step 1 is p − 2 when at most one of Case 1 and Case 3 applies (configuration (Y) again), and p − 1 when both Case 1 and Case 3 apply. The latter case is fixed using property (P) below.
We now finish the two unresolved cases, both of which occur when Case 1 applies and y m p = y m h 0 (recall that the pinnacles with indices h 0 , . . . , h m−1 are inherited from one execution of Case 2 to the next one, by affirmation (ii) above).
(P) If Case 1 applies and in Case 2 we have h 0 = p, then y m p is the highest pinnacle in π m .
Indeed, since Case 1 applies, in π we have y t < v 1 , for all t ≥ i. Thus in π (see Case 1) we have y u < v p+1 = v 1 for all u ≤ s, where y s = y i . When π is renamed as π 0 , we have: Moreover, since Case 2 applies with h 0 = p, we have that: By (1), y 0 u < v 0 p+1 and by definition v 0 p+1 < y 0 p , thus y 0 u < y 0 p for all u ≤ s, where y 0 s = y i . By (2) and (3), y 0 r < v 0 1 < y 0 p for all r with s ≤ r < p. Thus y 0 p is the highest pinnacle in π 0 . Due to affirmation (ii) above, y m p is the highest pinnacle in π m and property (P) is proved. Now, property (P) applies in each of the two unresolved cases and yield configuration (X). Lemma 1 is proved.
Once the lowest pinnacle is placed first, i.e. it is y 1 , each of the other pinnacles is easily placed. The reasoning is by induction.
Lemma 2. Assume that y 1 , y 2 , . . . , y k are the k lowest pinnacles, with k ≥ 1, and assume y t with t = k + 1 is the next lowest pinnacle. Then there is a balanced reversal allowing to replace y k+1 with y t , which does not modify the pinnacles y s , with s ∈ {1, . . . , k, t + 1, . . . , p}.
Proof. We notice that y k+1 > y t > y k > v k+1 . With e = cutA π (y t , v k+1 , y k+1 ), the reversal ρ(Next π (e), y t ) is balanced (type A.2s if Next π (e) = y k+1 and type C.3 otherwise) and moves y t at the sought place.  Table 2 indicates the reversals needed to achieve step 1, according to Algorithm 1.
Lemmas 1 and 2 allow to deduce the following result.

Proposition 2.
There is a sequence R 1 of at most one reversal (when p = 2), and at most 2p − 4 reversals (when p ≥ 3) allowing to order the pinnacles of π in increasing order.

Step 2: Place the wished dells
Now we replace v 1 , v 2 , . . . , v p+1 respectively with the lowest, the second lowest etc. element which is not a pinnacle, in order to have in π the same dells as in Id S . To this end, we need the following technical lemmas.

Lemma 3.
Assume v i ≤ v q , with i ≤ q, such that Prec π (v i ) is not a pinnacle and satisfies y 0 = Prec π (v i ) < y q . Then there exist two balanced reversals transforming π into π such that the only differences between π and π are the following ones: Proof. Let u = Prec π (v i ). See Algorithm 2. i) If u > v q then let e = cutA π (u, v q , y q ). Then ρ(u, e) is a balanced reversal (type B.1 if e = v q or type B.3s otherwise), since u < Next π (e) and e < u < Prec π (u), both by the definition of the cutpoint e. When applied, this reversal yields a permutation π where v i = v q , e ∈ D π (y i−1 , v i ), Prec π (e) = Prec π (u), v q = v i and Next π (v i ) = u. Then ρ(e, v i ) is a balanced reversal (type B.3s if e = v q and type C.2 otherwise) in π for we have e < Next π (v i ) = u by the definition of the cutpoint e, and v i < u < Prec π (u) = Prec π (e). The resulting permutation π satisfies the conditions in the lemma.
ii) If u < v q then ρ(v i , v q ) is a balanced reversal (type C.2) since Prec π (v i ) = y i−1 , and we have v i < u < v q < Next π (v q ) whether Next π (v q ) = y q or not. In the permutation π resulting once Then ρ(u, v i ) is a balanced reversal (type C.2) in π . To see this, we need to show that v i < Prec π (u) which is true since Prec π (u) > u > v i in π, and that u < Next π (v i ) which is also true since Input: A permutation π ∈ S n , pinnacles v i , v q satisfying the hypothesis of Lemma 3. Output: The permutation π obtained according to Lemma 3.
is not a pinnacle and satisfies and Next π (v i ) < y q−1 . Then there exist two balanced reversals transforming π into π such that the only differences between π and π are the following ones: Proof. Let u = Next π (v i ). See Algorithm 3. i) If u > v q then let e = cutD π (u, y q−1 , v q ) et f = Prec π (e). Then ρ(u, f ) is a balanced reversal (type A.1 if f = y q−1 , type A.2s otherwise), since u > e and f > u > Prec π (u), both by the definition of a cutpoint and whether f = y q−1 or not. When applied, this reversal yields a permutation π where v i = v i , Next π (v i ) = f with f ∈ A π (v i , y q−1 ) or f = y q−1 , as well as v q = v q , y q−1 = y i , and u ∈ D π (y i , v q ) with Next π (u) = e and Prec π (u) = Next π (u). Let t = Next π (u). Then t = Prec π (u) and ρ(f, t) is a balanced reversal in π (type A.1 if t = y i and f = y q−1 , type A.2 or A.2s if exactly one equality holds and type C.3. otherwise). Indeed, in all types but A.2 we need to show that t > Prec π (f ) and this is true since Prec π (f ) = v i = v i < u < t, because v i , u, t occur in this order on A π (v i , y i ). Moreover, types A.1, A.2 and C.3 require that f > Next π (t), which is true since Next π (t) = u and u < f by the definition of the cutpoint e. The permutation π resulting once ρ(f, t) is performed satisfies the conditions in the lemma.
ii) If u < v q then ρ(u, Prec π (v q )) is a balanced reversal (type A.1 if Prec π (v q ) = y q−1 , type A.2s otherwise). Indeed, with the notation s = Prec π (v q ), in case that s = y q−1 the reversal is of type A.1. and we have Next π (s) = v q and Prec π (u) = v i , so that the conditions in type A.1 are trivially verified. If s = y q−1 we have to check for type A.2s that s > Prec π (u), which is true as s > v q > u > v i = Prec π (u). In the permutation π resulting once ρ(u, s) is applied, and Next π (u) = v q . With t = Next π (u), we also have that t = Prec π (u). Then, in π , ρ(s, t) is a balanced reversal (type A.1 if t = y i and s = y q−1 , type A.2 or A.2s if exactly one equality occurs, resp. type C.3 if both equalities occur). Type A.1 is trivially verified, types A.2 and A.2s are guaranteed by s > v q > u respectively t = Next π (u) > u = Next π (v i ) > v i , whereas type C.3 is guaranteed by the latter two conditions together. Once this reversal is applied, the resulting permutation π satisfies the lemma.

Algorithm 3 ApplyLemma4
Input: A permutation π ∈ S n , pinnacles v i , v q satisfying the hypothesis of Lemma 4. Output: The permutation π obtained according to Lemma 4.
Proposition 3. Let π ∈ S n be a permutation with pinnacles y 1 < y 2 < . . . < y p . There is a sequence R 2 of at most 2p + 2 balanced reversals that places the dells in Id S as dells of π, in increasing order from left to right, without modifying the pinnacles of π (nor their order).
Proof. Assume the lowest k < p+1 dells (k = 0 is admitted here) are correctly placed as v 1 , v 2 , . . . , v k respectively, and let w be the next lowest element in π which is not a pinnacle. Then w must replace v k+1 . We have that w < y k since w < v k+1 < y k .
Then either w is adjacent to a dell among v 1 , v 2 , . . . , v k , or w is itself a dell. In all the other cases, a smaller element would be found, contradicting the choice of w.

Case 1. w is adjacent to a dell
Let w = Prec π (v j ) or w = Next π (v j ) for some j with 1 ≤ j ≤ k. Then we use Lemma 3, respectively Lemma 4 with i = j and q = k + 1. We have that v j < v k+1 by the minimality of the elements v 1 , v 2 , . . . , v k . We also have w < y k as proved above, and thus w < y k+1 by the increasing order of the pinnacles. So the hypothesis of the appropriate Lemma is satisfied. As w < v k+1 by the minimality of w, item ii) of the lemma holds. Consequently, after two balanced reversals, π is the same as π except that w has been removed from its place and has been placed before or after v k+1 (depending on which lemma is applied), thus becoming the dell v k+1 . Then we are done in this case.

Case 2. w is a dell
If w is already a dell, let w = v j with k + 1 < j ≤ p + 1. Then w < v k+1 by the minimality of w and v k+1 < y j since v k+1 < y k+1 ≤ y j . Let e = cutA π (v k+1 , v j , y j ) and, if it exists, f = Next π (e). Then ρ(v k+1 , e) is a balanced reversal (type B.3 if e = v j and type C.2 otherwise) since e = y j (v k+1 is an intermediate value among them), e < v k+1 < Prec π (v k+1 ) and v k+1 < f by the definition of the cutpoint e. In the permutation π resulting once ρ(v k+1 , e) is applied, v k+1 = v j , y k+1 = y j−1 , y j−1 = y k+1 , v j = v k+1 and y j = y j . If k + 1 = j − 1, then the order of the pinnacles does not change since the reversed block contains a unique pinnacle. In this case we are done. Otherwise, due to k + 1 < j − 1 we deduce that y k+1 < y j−1 and thus v k+1 < y k+1 < y j−1 . The cutpoint defined as e = cutA π (y k+1 , v j , y j−1 ) satisfies then the condition e ∈ {v j } ∪ A π (v j , y j−1 ). As a consequence, with f = Next π (e ) we have that ρ(f , y k+1 ) is a balanced reversal (type A.2s if f = y j−1 , type C.3 otherwise). The required conditions are fulfilled since both types need y k+1 > Prec π (f ) and this is true by the definition of the cutpoint e , which is Prec π (f ); and in type C.3 we moreover need f > Next π (y k+1 ) and this is true too by the definition of the cutpoint e , since f > y k+1 > Next π (y k+1 ). The resulting permutation π has v k+1 = v k+1 = v j and the pinnacles are in increasing order.
Input: A permutation π ∈ S n with pinnacle set S of cardinality p. The pinnacles of π are increasingly ordered. Output: The permutation π with pinnacles still in increasing order, and whose dells have become equal to the dells of Id S , in increasing order (Proposition 3) 1: for k = 0 to p do 2: w ← min({1, 2, . . . , n} − S − {v 1 , . . . , v k }) // x is the lowest wished dell 3: if ∃v j such that w = Prec π (v j ) then 4: π ← ApplyLemma3(π, v j , v k+1 )

4.3
Step 3: Move the remaining elements towards the place they occupy in Id S It remains to move in π the elements from each ascending and each descending set towards the end of the permutation. Proposition 4. Let π ∈ S n be a permutation with pinnacles y 1 < y 2 . . . < y p , and dells v 1 < v 2 < . . . < v p+1 which are the p + 1 lowest values in {1, 2, . . . , n} \ S. There is a sequence R 3 of at most 2(n − 2p) − 1 balanced reversals that transforms π into Id S .
Proof. This is done as follows. By hypothesis, v p+1 is smaller than all the elements from each ascending and each descending set, since the dells are the smallest elements that are not pinnacles. a) As long as Next π (v p+1 ) < y p , we use the following trick to move Next π (v p+1 ) towards D π (y p , v p+1 ) without changing the rest of π. Let π rev be the permutation obtained from π by reversing the whole π. Lemma 3 i) may be applied to π rev with i = q = p + 1 in order to move Prec π rev (v p+1 ) towards A π rev (v p+1 , y p ) using two balanced reversals (recall that y p > Next π (v p+1 ) = Prec π rev (v p+1 ) by the hypothesis above). Now, if we apply the balanced reversals with the same endpoints in π (without reversing the whole permutation), we obtain that Next π (v p+1 ) is moved towards D π (y p , v p+1 ) without changing the rest of π. The resulting permutation is still called π and we continue. When the process is finished, each t ∈ A π (v p+1 , y p+1 ) satisfies t > y p . b) For each i ≤ p, as long as Next π (v i ) = y i , use Lemma 4 to move Next π (v i ), which is smaller than y i and thus smaller than y p , towards D π (y p , v p+1 ), without changing the rest of π. More precisely, item i) in the lemma is used, by the minimality of v p+1 . When this step is finished, we c) If Next π (y p ) = v p+1 , the reversal ρ(Next π (y p ), v p+1 ) is balanced (type B.3s), since Next π (y p ) < y p < Next π (v p+1 ) by the constructions in the two previous items above, and v p+1 < Prec π (Next π (y p )) = y p . Then, in the new permutation still denoted π, D π (y p , v p+1 ) = ∅.
d) For each i ≤ p, as long as Prec π (v i ) = y i−1 , use Lemma 3 to move Prec π (v i ) towards A π (v p+1 , y p+1 ) (item i) in the lemma) without changing the rest of π. When this step is fin- It is easy to see that the result of these transformations is Id S . Indeed, item b) ensures that v i immediately precedes y i , for each pinnacle y i , 1 ≤ i ≤ p. Once step b) is performed, the elements in D π (y p , v p+1 ) are smaller than y p whereas by item a) those in A π (v p+1 , y p+1 ) (if any) exceed y p . The reversal in item c) thus only makes y p adjacent to v p+1 by concatenating the elements in D π (y p , v p+1 ) to those in A π (v p+1 , y p+1 ). Finally, item d) ensures that each y i is adjacent to each As a consequence, all elements but the p pinnacles and the p + 1 dells are possibly moved in items a), b) and d) using Lemma 3 or Lemma 4, thus performing two reversals per element. Since in item c) only one reversal is performed, the total number of reversals is at most 2(n − p − (p + 1)) + 1 = 2(n − 2p) − 1. Table 2.
The number of reversals is therefore bounded by 2n − min{p, 3} in all cases if p ≥ 1.

Remark 2.
In the case where p = 0, Steps 1 and 2 in the algorithm are not performed.
Step 3 reduces to items c) and d) which perform 1 and respectively at most 2(n − 1) reversals. The total number of reversals is thus upper bounded by 2n − 1 in this case.
so-called log-lists. Log-lists may be assimilated, with a view to simplification, to double-linked lists in which a collection of operations may be performed in O(log n). The ones we are interested in here are: given (a pointer on) x, compute (the pointers on) Prec π (x) and Next π (x); change the sign (positive or negative) of all the elements of a sublist; perform a reversal (and update the structure). The data structure we propose for the implementation of our algorithm combines log-lists, binary search trees (BSTs, for short), arrays and pointers. The shape of the permutation π is stored in a log-list L. For each pinnacle y i in the shape, two pointers toA and toD go towards the roots of two BSTs. BST toA (respectively toD) contains the elements in A π (v i , y i ) (respectively in D π (y i , v i+1 )) represented as pairs (π a , a). The order between pairs is defined as the standard (increasing) order between their left values (the elements). This implies that the pairs are also ordered according to the increasing order of their right values (the indices) in toA, respectively according to the decreasing order of their right values (the indices) in toD. Both of them, elements and indices, are used by the algorithm. An array P of pointers contains, in this order, the pointers to the pinnacles in L in increasing order of the pinnacles, followed by pointers to the wished dells (the dells of Id S ), in increasing order of the dells. The wished dells belong either to L or to one of the 2p BSTs pointed by the pointers toA, toD of each pinnacle.
With this data structure, we have: • All the operations except finding the leftmost pinnacle larger than a given value and with index larger than a given value (lines 4 and 12 of Algorithm 1) take O(log n) time each. This is quite easy, given the abovementioned properties of a log-list and those of a BST (searching a value in a BST, cutting a BST, merging two BSTs take O(log n) time). It is important to notice that a reversal is performed on the shape of the permutation (thus in the log-list L), but: 1) it cuts either toA or toD at each endpoint of the reversed block (except when the endpoint is a pinnacle or a dell) and recombines the resulting BSTs (also modifying the dells at the endpoints if needed) once the reversal is performed, both in O(log n) time; 2) it swaps the roles of toA and toD for each pinnacle situated strictly inside the reversed block, and thus each reversal must be followed by a sign change (in O(log n) time) in the reversed block of L (whose meaning is that when the value of the pinnacle is positive, toA and toD correspond respectively to the ascending and descending sets neighboring the pinnacle, whereas when the value is negative their roles are swapped).
• The operation of finding the leftmost pinnacle larger than a given value and with index larger than a given value (lines 4 and 12 of Algorithm 1) cannot be implemented in O(log n) time per operation. Instead, it may be shown that all these operations (in lines 4 and 12 of Algorithm 1) may be implemented altogether in O(n log n) time.
To this end, one has to remember that in Lemma 1 when Case 2 (tested in lines 4 and 12) is applied to y i , one finds a first index h > i such that v 1 < y h . Then we reverse the block with endpoints v 1 and v h . Again as proved in Lemma 1, the values h q computed similarly with h in the next executions of the while loop (lines 9-17 in Algorithm 1) belong to the block of π situated between v 1 and y h , and become smaller at each execution. That means each reversal due to Case 2 reverses a proper prefix of the block resulting after the latest reversal. Then it is sufficient to perform in O(n log n) time a pre-treatment of the block of the (initial) permutation π with endpoints v 1 and y h (once h is known). This pre-treatment affects to each dell v j with j < i a pointer to the leftmost pinnacle y h such that y h > v j and i < h < h, and symmetrically affects to each dell v j with j > i a pointer to the rightmost pinnacle y h such that y h > v j and h < i. By the previous considerations, the reversals performed by the algorithm do not affect the meaning of the pointers affected to the dells v q 1 obtained during the execution of Case 2. These pointers, computed independently from the data structure presented above, may be stored as additional information in the cells representing the dells in the log-list. They allow to find the pinnacles y q h q in O(1) time each, and thus the dells v h needed by Algorithm 1 (line 13) in O(log n) time each.

Conclusion
We have shown in this paper that the BALANCED SORTING problem has a solution using at most 2n − min{p, 3} balanced reversals (when p ≥ 1), for each permutation of n elements. This is an upper bound, but many permutations may be sorted using less balanced reversals. Then we can ask: Question 1. (MINIMUM BALANCED SORTING) Given π ∈ S n with pinnacle set S, find the minimum number of balanced reversals needed to transform π into Id S .
A similar question may be asked when transforming a permutation π into a permutation π with the same pinnacle set: Question 2. (MINIMUM BALANCED TRANSFORMATION) Given π, π ∈ S n with the same pinnacle set S, find the minimum number of balanced reversals needed to transform π into π . This question is different from the previous one, since the identity permutation is not necessarily an intermediate permutation in the transformation of π into π .
A related problem is raised by Example 3. Even when a set is admissible as the pinnacle set of a permutation, the order of the pinnacles in the permutation is important, since some orders may be impossible to respect. Identifying these orders could be a way to better target the balanced reversals yielding a minimum sorting.
Question 3. Let S be an admissible pinnacle set. Give a characterization of the total orders σ on S such that a permutation π ∈ S n exists whose sequence of pinnacles read from left to right is exactly σ.
Note that Davis et al. [5] study the number of permutations on n elements with a given admissible set S, and give recursive formulas for it. This is a related question, which involves however supplementary combinatorial aspects related to the elements not belonging to S.