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)