Optimal Dislocation with Persistent Errors in Subquadratic Time

We study the problem of sorting N elements in the presence of persistent errors in comparisons: In this classical model, each comparison between two elements is wrong independently with some probability up to p, but repeating the same comparison gives always the same result. In this model, it is impossible to reliably compute a perfectly sorted permutation of the input elements. Rather, the quality of a sorting algorithm is often evaluated w.r.t. the maximum dislocation of the sequences it computes, namely, the maximum absolute difference between the position of an element in the returned sequence and the position of the same element in the perfectly sorted sequence. The best known algorithms for this problem have running time O(N2) and achieve, w.h.p., an optimal maximum dislocation of O(logN)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$O(\log N)$\end{document} for constant error probability p. Note that no algorithm can achieve maximum dislocation o(logN)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$o(\log N)$\end{document} w.h.p., regardless of its running time. In this work we present the first subquadratic time algorithm with optimal maximum dislocation. Our algorithm runs in O~(N3/2)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$\widetilde {O}(N^{3/2})$\end{document} time and it guarantees O(logN)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$O(\log N)$\end{document} maximum dislocation with high probability for any p ≤ 1/16.


Introduction
We study the problem of sorting N distinct elements under recurrent random comparison errors.In this classical model, each comparison is wrong with probability at most p, where p < 1/2 is some fixed constant (some works further assume p < 1/16, as we also do here).The probability of errors are independent over all possible pairs of elements, but errors are recurrent: Repeating the same comparison several times is useless since the result is always the same, i.e., always wrong or always correct.
Because of errors, different sorting algorithms can have different guarantees to output a "nearly sorted" sequence.To measure the quality of an output sequence in terms of sortedness, a common way is to consider the dislocation of an element, which is the absolute difference between its position in the output and its position in the correctly sorted sequence.In particular, one can consider the maximum dislocation of any element in the permutation or the total dislocation of a permutation, i.e., the sum of the dislocations of all n elements.Of course, the running time is also an important criterion for evaluating sorting algorithms.
Regarding the maximum dislocation and the running time, in the recurrent random comparison errors, this is the state of the art: -Several algorithms [3,10,13] guarantee maximum dislocation O(log N) with high probability (w.h.p.), though their running time is quadratic or even larger (see Table 1).-No algorithm (even randomized) can achieve maximum dislocation o(log N) with high probability or total dislocation o(N) in expectation, regardless of its running time [10].
This suggests naturally the following question: Is there any algorithm with subquadratic running time which achieves optimal maximum dislocation O(log N) with high probability?
In this paper we give an affirmative answer to this question for p < 1/16.We indeed present the first subquadratic time algorithm with optimal maximum dislo-Table 1 The existing approximate sorting algorithms and our result.The constant c(p) in [3] depends on both the error probability p and the success probability of the algorithm, and it is typically quite large  1).Intuitively speaking, our algorithm (RECURSIVE WINDOW SORT) first picks a random permutation of the input elements and then recursively performs a number of deterministic operations which use the WINDOW SORT algorithm in [10] as a subroutine (more precisely, a minor modification of it, described in Section 1.2).All recursive steps are deterministic and they consist of an algorithm that approximately sorts an input sequence whenever the comparison errors do not depend on the elements' positions in the sequence (hence the initial permutation).The correctness of RECURSIVE WIN-DOW SORT combines a technical condition that the algorithm in [10] guarantees, together with an intermediate "merge step" which works well on random permutations of the input (see Section 2 for a high level description of the algorithm and the main ideas).

Related Work
The algorithm by Braverman and Mossel [3] constructs the maximum likelihood permutation, whose computation requires a rather large (though polynomial) running time.Their method in fact uses only O(N log N) comparisons and is applicable to by Rubinstein and Vardi [15].The faster algorithms by Klein et al. [13] and Geissmann et al. [10] work for p smaller than some absolute small constant (e.g., p < 1/16 in [10]).Ajtai et al. [1] provide algorithms using subquadratic time (and number of comparisons) when errors occur only between elements whose difference is at most some fixed threshold.Damaschke [7] gives also a subquadratic time algorithm, by assuming that at most k errors occur.The algorithm returns a sequence with up to O(k) inversions; it is based on finding a solution for the feedback arc set (FAST) problem.Coppersmith and Rurda [6] provide a simple 5-approximation algorithm for the weighted FAST problem, if the weights satisfy probability constraints.
An easier error model is the one with non-recurrent errors, meaning that the same comparison can be repeated and the errors are independent with some probability p < 1/2.For this model, Feige et al. [8] gave a sorting algorithm running in O(N log(N/q)) steps, where 1−q is the success probability of the algorithm.Alonso et al. [2] and Hadjicostas and Lakshamanan [12] studied the classical Quicksort and recursive Mergesort algorithms, respectively.Sorting by repeatedly performing random swaps results in Markovian processes which have been studied by Geissmann et al. [9,11].
Finally, computing with errors is often considered in the framework of a twoperson game called Rényi-Ulam Game (see Pelc's survey [14] and Cicalese's monograph [5]).

Reviewing WindowSort
In this section, we describe the key features of the WINDOW SORT algorithm [10], which will be used as a subroutine of our main algorithm (see Algorithm 1).The algorithm iteratively "refines" the current solution by comparing each element only with the elements in a "window" of a certain size.A key feature of the algorithm is that, if the input sequence has already a "small" maximum dislocation, then it is possible to start the algorithm with a "small" window size to reduce the overall running time and still get optimal maximum dislocation with high probability (see Lemma 1 for the formal statement).
We consider the problem of sorting N distinct integers, which, under the error model considered here, is equivalent to sorting a sequence containing the integers {1, 2, . . ., N}.For any sequence S, and any element x in the sequence, we define r(x, S) as the number of elements smaller than x in S. Note that this gives the correct position of x (i.e., r(x, S) + 1) in the correctly sorted sequence, and it only depends on the elements in S (not on the sequence order).In the following we will use κ ≥ 1 to denote a global constant that only depends on the error probability p.For ease of presentation, we assume that p ≤ 1 32 , although our algorithm can be adapted to work for p < 1 16 (which is a condition needed to successfully run WINDOW SORT).We say that a comparison between a pair of elements x, y, with x < y, is an error if x is (incorrectly) reported to be larger than y.The following lemma summarizes the properties of WINDOW SORT.

Lemma 1 (essentially due to [10]) Consider a set U of n input elements that are subject to comparison errors according to the above error model. Let S be an arbitrary permutation of U (possibly chosen as a function of the comparison errors) and let D be the maximum dislocation of S. With probability at least 1 − 1 n 8 the following holds: If W ≥ D, then WINDOW SORT with inputs S and W returns, in O(|S| • W ) time, a sequence (permutation of S) having maximum dislocation at most κ log n and expected total dislocation O(n).
The proof of the lemma above follows from the analysis of WINDOW SORT in [10] with some minor modifications due to the fact that we start our algorithm with some window size W (while the original one starts with W = Θ(n) assuming a worst-case dislocation).This analysis in [10] essentially considers a single iteration of the outer while loop and shows that, with high probability, the following holds: For any S with maximum dislocation at most w ≥ κ log n, the sequence S in Algorithm 1 has maximum dislocation at most w/2.
Since we require a slightly better success probability (namely, 1 − 1 n 8 instead of 1 − 1 n in [10]) we shall use a larger constant κ compared to the original constant in [10].More in detail, the analysis of [10] proves that: 1.The dislocation of an element x in S is at most errors(x, w), where errors(x, w) denotes the number of errors in the comparisons between x and all elements in the interval [x − 4w, x + 4w] (Lemma 4 in [10]).
2. For any element x ∈ S, the undesirable event "errors(x, w) > w/2" happens with probability at most 1/n 3 , provided that w > 2f (p) log n for a suitable constant f (p) (Lemmas 10 and 13 in [10]).The lower bound of 1 − 1 n on the success probability follows by using the union bound over all the (at most n 2 ) possible choices of x and w.In our case, the probability of the event "errors(x, w) > w/2" can be lowered to 1/n 12 by replacing 2f (p) with 8f (p) in the original proof.The same union bound argument yields a success probability of at least 1 The remaining of the analysis in [10] is unchanged and Lemma 1 of the current paper follows from Theorems 9 and 14 of [10] (where the original constant κ = 9f (p) is now replaced by κ = 36f (p) ≥ 1).
As our new algorithm will make several calls to WINDOW SORT as a subroutine, we would like that each of these calls "succeed" in the sense of Lemma 1: Definition 1 We say that a call to WINDOW SORT succeeds if the implication in Lemma 1 is satisfied, namely if either (i) the maximum dislocation of S is larger than W , or (ii) the maximum dislocation of the returned sequence is at most κ log n.

Warm Up: A Simple Two-Level Scheme
In this section we informally describe some of the ideas used in our new algorithm.We consider a simplified (non-recursive) version of it.While this version already results in a subquadratic time algorithm, it will have an asymptotically worse running time than the general (recursive) algorithm described in Section 3. The simplified algorithm is depicted in Fig. 1; it consists of the following steps: Fig. 1 The two-level scheme 1. Start with a random permutation S of the input sequence and partition this sequence S into η blocks B 1 , B 2 , . . ., B η of the same size N/η.2 2. Run WINDOW SORT with window size N/η on each block B i to obtain a sequence S i .3. Combine all the sequences S i together into a sequence S according to the following "matrix transposition shuffle": Enter the η blocks S 1 , . . ., S η as columns in an (N/η) × η matrix, then read the entries out row by row.Hence, the first element in each S i will be placed in one of the first η positions of S , the second element in each S i will be placed in a position between η + 1 and 2η in S , and so on.4. Run WINDOW SORT with window size W on this new sequence S .This simple scheme is fully specified by the number η of blocks (and thus their size N/η), and by the initial window size W in the last call to WINDOW SORT.Both these parameters need to be chosen carefully in order to achieve the desired performance.

Saving in the Running Time
In the first part the WINDOW SORT algorithm is invoked on η blocks of size N/η, and hence the total running time is O(N 2 /η), since each block takes O((N/η) 2 ) time.The final call to WINDOW SORT can be fast if S has already a bounded maximum dislocation, since in this case we can start the algorithm with a small initial window size W (see Lemma 1).The overall running time is then O(N 2 /η + W N). As soon as we can have η not too small and W not too large, this scheme improves over the quadratic running time of the original WINDOW SORT algorithm.In particular, as we discuss in the next paragraph, S will have maximum dislocation O( √ Nη) and thus we can set W = O( √ Nη).The overall running time becomes O(N 2 /η + ηN 3/2 ), which for η = Θ(N 1/4 ) yields an algorithm with O(N 7/4 ) running time.

Need for Initial Random Permutation
To see why we perform the initial randompermutation step, consider the version in which we start with the sorted sequence, that is, S = 1, 2, . . ., N .We claim that, in this case, it is very likely that the sequence S obtained after recombining the blocks has large dislocation.Indeed, suppose that all the calls to WINDOW SORT sort each block perfectly.Then S 1 = 1, 2, . . ., N/η , S 2 = N/η + 1, . . ., 2N/η , and so on, causing element N/η to be placed in S in position at least The initial random-permutation step ensures that consecutive input elements (w.r.t.their true ordering) will likely end up in different blocks, causing the dislocation of S to be O( √ Nη), which allows us to set W = O( √ Nη) in the last call to WINDOW SORT.Finally, we remark that the initial step where we pick a random permutation of the input (the elements to be sorted) can be implemented more efficiently by distributing these elements directly into the desired number of blocks.

Idea of the Correctness Argument
For the above choice of parameter W to work, we need to show that all the calls to WINDOW SORT will succeed with high probability.As far as the calls on the blocks B 1 , . . ., B η are concerned (step 2 of the two-level scheme), we notice that we only require η = O(N 7 9 ).Indeed, by Lemma 1, we know that each of the these η invocations of WINDOW SORT fails with probability at most O(|B i | −8 ) = O (η/N) 8 .A simple application of the union bound shows that all the calls succeed with probability at least 1 − O η 9 N 8 = 1 − O 1 N .Regarding the final call to WINDOW SORT on sequence S (step 4 of the twolevel scheme), it is worth noticing that the errors between pairs of elements in S now depend on the position of the elements (since S is obtained by performing a matrix transposition shuffle on the sequences S 1 , . . ., S η which, in turn, depend on the comparison errors through the execution of WINDOW SORT).Nevertheless, since the sequence S in Lemma 1 is allowed to be any permutation of the input elements, we are still able to conclude that the final call to WINDOW SORT will succeed with probability at least 1

The Algorithm
We now describe the full version of our algorithm, which we call RECURSIVE WIN-DOW SORT.Intuitively, our algorithm is a recursive version of the scheme described in Section 2 (see Fig. 1).We initially perform a random shuffling of the input sequence once and for all to obtain a sequence S (like in the two-level scheme).Then the basic idea is to introduce several levels of recursion, building larger and larger blocks of approximately sorted subsets, where for the combination of the results of recursive calls the scheme from Section 2 is used (shuffling according to matrix transposition, and then a call to WINDOW SORT with an appropriate window size).Note that all these steps are deterministic, and a careful choice of the block sizes on the different levels is essential.
We describe the recursive steps of RECURSIVE WINDOW SORT.We denote by N the total number of elements to sort.The behavior of our recursive algorithm varies according to the current depth of recursion.More precisely, we index the levels of recursion from 0 to h = log N, where 0 denotes the initial recursive call and h corresponds to the maximum recursion depth.Intuitively, each recursive call aims at approximately sorting the elements in its input sequence S. A recursive call at depth d = h achieves this by directly invoking WINDOW SORT on S with window size n h = |S|.Therefore, we will now focus on a generic recursive call at depth d < h.First, the sequence S is split into disjoint blocks of β d elements (the value of β d will be chosen below); The algorithm is then recursively invoked on each of these blocks; The elements in the blocks (that are now approximately sorted) are rearranged into a new sequence S using the "matrix transposition shuffle" described in Section 2; Finally, the algorithm returns the sequence obtained by invoking WINDOW SORT with window size W on S .The pseudocode of RECURSIVE WINDOW SORT and its recursive step can be found in Algorithms 2 and 3, respectively.
In order to optimize the running time, we define two auxiliary quantities we set n 0 = N and, for d = 0, . . ., h − 1, we choose the remaining parameters as follows: In the following two subsections we analyze the running time and the maximum dislocation of our algorithm, respectively.

Analysis of the Running Time
We begin by providing an upper bound on the running time of RECURSIVE WINDOW SORT.

Lemma 2 The running time of
Proof The running time of WINDOW SORT on an instance of n elements with starting window size W is O(nW ) (see Lemma 1).The time needed for one execution of RECURSIVE STEP at depth d < h, excluding its recursive calls, is dominated by the time required by the final call to WINDOW SORT, i.e., it is O(n d W ). Since the elements in the input sequences of the calls of RECURSIVE STEP at a fixed depth are pairwise disjoint, the number of calls at depth d is O N n d , implying that the overall running time for those calls is Similarly, the cost of a single call at depth h = log N is O(n 2 h ) and the overall cost of all calls at depth h is . By summing the above upper bounds over all h+1 = 1+log N levels of recursion, we can conclude that the running time of RECURSIVE WINDOW SORT is

Analysis of the Dislocation
We now prove that RECURSIVE WINDOW SORT guarantees maximum dislocation O(log N) with high probability.The next lemma shows that all the calls to WINDOW Proof We focus on an arbitrary single call to WINDOW SORT performed during the execution of RECURSIVE WINDOW SORT.Let S be the input sequence to the execution of RECURSIVE STEP performing the call, and let S be the input to the WINDOW SORT call itself.Notice that the set of elements in S does not depend on the errors (indeed, while their order depends on the previous steps of the algorithm, the unordered version of S with the unordered version of S, which is uniquely determined by the initial random permutation).Since the elements in S are a random subset of the original input elements to RECURSIVE WINDOW SORT (and this set does not depend on the errors among the elements), we still have that the errors between pairs of elements in S happen independently at random, with probability p.Therefore, by Lemma 1, the considered execution of WINDOW SORT succeeds with probability To conclude the proof, it suffices to notice that the total number of calls to WIN-DOW SORT performed by RECURSIVE WINDOW SORT is at most O( √ N log N), since the depth of the recursion is h = O(log N) and at most O( √ N) calls are performed by all the executions of RECURSIVE STEP for any fixed depth d (this follows from the above bound on n d and from the fact that the input sets S of all the executions of RECURSIVE STEP at depth d are pairwise disjoint).The claim follows by the union bound.
We now give a sufficient condition for which the following implication holds: If all executions at depth d + 1 return sequences of dislocation κ log N, then also the execution at depth d returns a sequence of dislocation at most κ log N. Definition 2 (GOOD BLOCKS) We say that an execution of RECURSIVE STEP at depth d < h has GOOD BLOCKS if the sequence S to which we apply the recursion satisfies the following condition: For every element , where B j is the block of length Note that the input of each execution (recursive call) of RECURSIVE STEP is a fixed subset of elements of the initial sequence which does not depend on the comparison errors.

Lemma 4 Consider an execution of RECURSIVE STEP at depth d < h and suppose that the following conditions hold:
1. execution has GOOD BLOCKS (Definition 2); 2. All the executions of RECURSIVE STEP at depth d + 1 return a sequence with maximum dislocation at most κ log N; 3. The call to WINDOW SORT performed by the considered execution of RECUR-SIVE STEP succeeds.
Then the considered execution returns a sequence with maximum dislocation κ log N.
Proof By hypothesis 3 in combination with Lemma 1, it suffices to show that the sequence S obtained before invoking WINDOW SORT has maximum dislocation at most W . Consider an element x ∈ S, let B i be the block containing x, and let L x be the number of elements preceding x in B i (its position in B i minus 1).By the hypothesis on the executions at depth d + 1, we know that |L x − L x | ≤ κ log N and, since the considered execution has GOOD BLOCKS, we can use the triangle inequality to write: where last inequality is due to κ ≥ 1.Let G x be the number of elements preceding x in S (its position minus 1), so that |G x − G x | is the dislocation of x in S .By definition of S and by the above inequality: and similarly: Using the definitions of n d and β d , together with the equality h = log n, we obtain:

Lemma 5
The probability that all executions of RECURSIVE STEP at all depths Proof Fix an element x and a depth d < h.Let S and B i be the sequence of size n d and its block of size β d = n d+1 containing x. Due to the initial random permutation, the set B i \ {x} is a random subset of n d /β d − 1 elements chosen from the n d − 1 elements in S \ {x}.We can therefore study the distribution of L x by considering the following experiment: An urn contains G x black balls (elements smaller than in S) and n d − G x − 1 white balls (elements larger than x in S).We draw β d − 1 balls at random and consider the number of drawn black balls (which corresponds to L x ).We hence have that L x is distributed as a hypergeometric random variable of parameters Hence the following holds for sufficiently large values of N: Notice that the overall number of elements x for which the above condition must hold is upper bounded by N log N. Indeed, there are h = log N recursion levels and each level defines a partition of the N elements into blocks (i.e., the total number of elements at each level is N).By the union bound, the probability that all the blocks are good is at least 1 − (N log N) 2  N 4 > 1 − 1 N 2 .
We can now invoke Lemma 4 in a recursive fashion in order to prove the main result of this section.Proof We assume that (i) all the calls to WINDOW SORT performed during the execution of RECURSIVE WINDOW SORT succeed, and (ii) all the recursive executions of RECURSIVE STEP have GOOD BLOCKS.By Lemmas 3 and 5 this happens with probability at least 1 − 2 N 2 ≥ 1 − 1 N .We prove the following claim by induction on i = h − d: all the executions of RECURSIVE STEP at depth d return a sequence having maximum dislocation κ log N.
If i = 0, then d = h.Consider any execution of RECURSIVE STEP at depth h and let S be its input.Notice that WINDOW SORT is invoked on S with window where the inequality |S | > √ N holds since either |S| = N (and consequently |S | = |S| = N) or |S | = |S| = n d for some d = 1, . . ., h, implying:

Theorem 1 2 )
For any set of N input elements under the considered error model, and for any permutation S of these elements (possibly depending on the comparison errors), RECURSIVE WINDOW SORT returns a permutation of S with maximum dislocation κ log N with probability at least 1− 1 N .Moreover, its running time is O(N 3 and the expected total dislocation of the returned sequence is O(N).
All the calls to WINDOW SORT performed by RECURSIVE WINDOW SORT succeed (jointly) with probability at least 1 − O 1 N 3 .