6

Sorry that I don't know any better way to express this question.

Assuming that we have a perfectly shuffled array of unique numbers, we know that the array is in fact consisted of ascending or descending sorted subarrays. In average howmany such sorted subarrays consist this one particular perfectly shuffled array is the question.

As a simple example, please consider an array of 20 integers [0...19] shuffled as [14,2,5,16,18,19,7,6,12,17,3,4,8,11,15,13,1,0,9,10] which is internally sorted in chunks as [[14,2],[5,6,18,19],[7,6],[12,17],[3,4,8,11,15],[13,1,0],[9,10]] so in total we obtain 7 subarrays.

Now this observation just paves the way for a very efficient sorting algorithm once you merge them cleverly.

Yet it's even better than that. I naively assumed that the number of subarrays in average should be like half the length of the initial array (50% sorted internally). But no, that's only the worst case which you very rarely get. It's like hitting 10101010 or 01010101 in 8 bits. So for 8 bits per se the chance of hitting worst case is $2\times\frac{1}{2^8} = \frac{1}{128}$. Similarly hitting the best case where we end up with only one subarray (given the shuffled array is already sorted ascending or descending) should have the same chance (or is it?). As a result, in average we get better than 50% internal sorting but howmuch better? In my tests I have observed that in average a perfectly shuffled array of unique items turns out to be ~58.68% sorted internally. In other words a 1000 item array would yield in average ~410 sorted subarrays to merge.

While my algorithm seems just fine, perhaps it misses some edge cases. I would appreciate helps to know if this 58.68% figure is inline with the math of it.

Edit: To gain further insight, I have decided to add some more observations when repeatition is allowed.

  • 2 choices in n length array i.e. (0,1) yields 75% (25 subarrays from 100 items array)
  • 3 choices in n length array (0,1,2) yields 67.28%
  • 4 choices in n length array yields 64.385%
  • 13 choices yields %60.06
  • n choices in n length array yields 58.68% (same as no repetition)
Redu
  • 231
  • I don't get the percentages. How much would your example array be "sorted internally"? – Christian Dec 09 '22 at 14:00
  • @Christian Let $n_i$ and $n_s$ be the number of items and number of subarrays respectively. Then I calculate the percentage as $(1-\frac{n_s-1}{n_i})\times 100$. Accordingly the example array would be $(1-\frac{7-1}{20})\times 100 =$ 70% sorted. – Redu Dec 09 '22 at 15:33

1 Answers1

3

I can prove that, as $n\to\infty$, the expected number of sorted sub-arrays in a random permutation of length $n$ grows asymptotically like $$ \big(1+2\cdot (\tan 1-\sec 1)\big)n\approx 0.4132\,n $$ Note that $1-0.4132=0.5868$, which matches the $58.68\%$ figure from your simulations.

Proof: Let the random permutation be $a=(a_1,a_2,\dots,a_n)$. Call $a_k$ a leader if it is the first entry of a sorted subarray. The number of subarrays equals the number of leaders, so to find the expected number of subarrays, we can add up the probabilities of each entry being a leader.

For each $k\in \{1,\dots,n\}$, let us find the probability that $a_k$ is a leader. Certainly, for $k\ge 4$, we know $a_k$ is a leader if one of these two events occurs: $$ a_{k-3}<a_{k-2}<a_{k-1}>a_k\qquad\text{or}\qquad a_{k-3}>a_{k-2}>a_{k-1}<a_k\tag1 $$ The probability of the first event is $$ \mathrm P(a_{k-3}<a_{k-2}<a_{k-1}>a_k)=\frac1{3!}-\frac1{4!}=\frac18=12.5\%, $$ and symmetrically the same is true for the other event, so the probability $a_k$ is a leader is at least $2\cdot 12.5\%=25\%$. However, there are other ways $a_k$ can be a leader. If $k\ge 6$, then $a_k$ is a leader as long as $$ \color{blue}{a_{k-5}<a_{k-4}<a_{k-3}}>\color{purple}{a_{k-2}<a_{k-1}} > \color{red}{a_k}\tag2 $$ This happens with probability $$ \mathrm P(a_{k-5}<a_{k-4}<a_{k-3}>{a_{k-2}<a_{k-1}}>a_k)=\frac{1}{3!}\cdot \frac1{2!}-\frac1{3!}\cdot \frac1{3!}-\frac1{5!}+\frac1{6!}\approx 4.86\% $$ Again, we need to multiply this by $2$ to account for the upside-down version of this situation. So far, the accumulated probability is $25\%+9.72\%\approx 34.7\%$.

In general, call a sequence alternating if every entry is either larger than its two neighbors, or smaller than its two neighbors. Then

$a_k$ is a leader if and only if the longest contiguous alternating sub-array ending with $a_k$ has odd length.

For example, if $(1)$ occurs, the maximal alternating subarray ending with $a_k$ is $(a_{k-2},a_{k-1},a_k)$, which has length $3$. If $(2)$ occurs, the maximal alternating subarray is $(a_{k-4},a_{k-3},a_{k-2},a_{k-1},a_k)$, with length $5$.

Let $A_n$ be the number of alternating permutations of length $n$ (note: my definition of $A_n$ gives double the number of Wikipedia's definition, since I allow both up-down-up-... and down-up-down-...). The probability that $a_k$ ends a maximal alternating sub-array of length $3$ is $A_3/3!-A_4/4!=25\%$, while the probability that the length is $5$ is $A_5/5!-A_6/6!=9.72\%$, and so on. The total probability that $a_k$ is a leader is $$ \frac{A_3}{3!}-\frac{A_4}{4!}+\frac{A_5}{5!}-\frac{A_6}{6!}+\dots\tag{$*$} $$ Technically, for $a_k$, the sum in $(*)$ should only include $k-2$ terms, but this does not matter in the limit as $n\to\infty$. André showed that, for any $x\in \mathbb C$ such that $|x|<\pi/2$, $$ \sum_{n\ge 0}A_n\frac{x^n}{n!}=2(\tan x+\sec x) $$ Letting $x=-1$, and rearranging, the sum of the series in $(*)$ is $$ -2(\tan(-1)+\sec(-1))+\frac{A_0}{0!}-\frac{A_1}{1!}+\frac{A_2}{2!}=1+2(\tan 1-\sec 1) $$ Since the probability of each $a_k$ being a leader is $1+2(\tan1-\sec 1)$, the expected number of sorted sub-arrays is $n$ times that probability.

Mike Earnest
  • 75,930
  • Thank you for the answer. Judging from the result it looks somewhat reasonable but some of your facts seem more like assumptions to me. Such as "Certainly, for k≥4, we know $a_k$ is a leader if one of these two events occurs. $a_{k-3}<a_{k-2}<a_{k-1}>a_k$ or $a_{k-3}>a_{k-2}>a_{k-1}<a_k$ Why do you exclude $a_{k-3}>a_{k-2}<a_{k-1}>a_k$ or $a_{k-3}<a_{k-2}>a_{k-1}<a_k$? Besides I cant make sure "ak is a leader if and only if the longest contiguous alternating sub-array ending with ak has odd length." is correct. Could you please show it in my example? – Redu Dec 11 '22 at 12:28
  • Also could you please elaborate a little bir how you calculate the probabilities. Such as, for a simple example, the probaility of $a_1$ of being a leader is 1 and $a_2$ is 0. This means we should always disregard the first two elements and add up probabilities from $a_3$ on. Could you please extend your answer to show how you calculate the probability $a_3$ to be a leader? – Redu Dec 11 '22 at 12:35
  • Nice proof! I worked on this for a bit myself, and I used the same first steps as you, but I got stuck on how to build the "$a_k$ is a leader if and only if the longest contiguous alternating sub-array ending with $a_k$ has odd length" observation into my formulas. – David Clyde Dec 11 '22 at 12:55
  • 1
    @Redu "Why do you exclude $a_{k-3}>a_{k-2}<a_{k-1}>a_k$"? For example, your inequalities are satisfied for $k=6$ when $(a_1,a_2,a_3,a_4,a_5,a_6)=(6,5,2,1,4,3)$, but $a_6=3$ is not a leader, because the subarrays are $[6,5,2,1]$ and $[4,3]$. The longest alternating sub-array ending with $a_6$ is $(2,1,4,3)$, with length $4$, even length. – Mike Earnest Dec 11 '22 at 14:40
  • 1
    In general, the exact probability that $a_k$ is a leader is $$\sum_{i=1}^{k}(-1)^{i+1}\frac{A_i}{i!}$$ This confirms $P(a_1\text{ is leader})=A_1/1!=1$, $P(a_2\text{ is leader})=A_1/1!-A_2/2!=0$, $P(a_3\text{ is leader})=A_1/1!-A_2/2!+A_3/3!=1/1-2/2+4/6=2/3$. But since we are only using $\sum_{k=1}^n P(a_k\text{ is leader})$, the exact values of particular $P(a_k\text{ is leader})$ do not matter, only the limit as $k\to\infty$ matters. – Mike Earnest Dec 11 '22 at 14:49
  • How about $(a_1,a_2,a_3,a_4,a_5,a_6)=(1,2,8,7,9,3)$ where the subarrays are [1,2,8],[7,9],[3] and $a_6=3$ gets the chance to be leader. What am i missing here? – Redu Dec 11 '22 at 15:31
  • 2
    @Redu The longest alternating subarray ending with $3$ is $(2,8,7,9,3)$. This has odd length. I was claiming that $a_k$ is a leader iff the length is odd, so there is no problem here. This is just case $(2)$ discussed in my post. – Mike Earnest Dec 11 '22 at 15:47