The devil

Bogosort, also known as stupid sort or monkey sort , is a rather (in)famous sorting technique known for its inefficiency. In bogosort, you shuffle the array randomly and check if it's sorted; if it isn't, you repeat the whole thing.

Since you're clever, you come up with a better version of bogosort. If some of the first and last few values (a prefix and a suffix of the array) are already correctly sorted, we don't need to include them in the shuffle; we can fix them in place. For example, suppose that we shuffle and get ( 1 , 2 , 6 , 4 , 5 , 3 , 7 ) (1,2,6,4,5,3,7) ; we can now fix the 1 , 2 , 7 1,2,7 and only shuffle 6 , 4 , 5 , 3 6,4,5,3 . Note that although 4 , 5 4,5 are in the correct place, we still include them in the shuffle since they are not "first and last few values".

Let X ( n ) X(n) be the expected number of shuffles needed to sort the first n n natural numbers, given that none of the numbers are in the right place initially (so all numbers are included in the shuffle). If X ( 15 ) X(15) can be written as A B \frac{A}{B} where A A and B B are coprime positive integers, what is the value of A + B A+B ?

Bonus Question What is the expected running time of the modified bogosort?

Not an original problem


The answer is 477626207975593.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Mark Hennings
Dec 17, 2015

If n 2 n \ge 2 , the number of orderings where the first u u (but not the first u + 1 u+1 ) are correct, and where the last v v (but not the last v + 1 v+1 ) are correct, is (by the Inclusion-Exclusion Principle): ( n u v ) ! ( n u v 1 ) ! ( n u v 1 ) ! + ( n u v 2 ) ! (n-u-v)! - (n-u-v-1)! - (n-u-v-1)! + (n-u-v-2)! provided that u + v n 2 u+v \le n-2 . Thus the probability that, when n n symbols are shuffled, precisely a a symbols are fixed, leaving n a n-a symbols to be shuffled next time around, is p n , a = { ( a + 1 ) [ ( n a ) ! 2 ( n a 1 ) ! + ( n a 2 ) ! ] n ! 0 a n 2 0 a = n 1 1 n ! a = n p_{n,a} \; =\; \left\{ \begin{array}{lcl}\frac{(a+1)\big[(n-a)! - 2(n-a-1)! + (n-a-2)!\big]}{n!} & \quad & 0 \le a \le n-2 \\ 0 & \quad & a = n-1 \\ \frac{1}{n!} & \quad & a = n \end{array} \right. Using conditional expectations (and setting X ( 0 ) = 0 X(0) = 0 for convenience) X ( n ) = a = 0 n p n , a ( 1 + X ( n a ) ) = 1 + a = 0 n p n , a X ( n a ) = 1 + a = 0 n 2 p n , a X ( n a ) = 1 + a = 0 n 2 ( a + 1 ) [ ( n a ) ! 2 ( n a 1 ) ! + ( n a 2 ) ! ] n ! X ( n a ) = 1 + a = 2 n ( n + 1 a ) [ a ! 2 ( a 1 ) ! + ( a 2 ) ! ] n ! X ( a ) = 1 + a = 2 n ( n + 1 a ) ( a 2 3 a + 3 ) ( a 2 ) ! n ! X ( a ) \begin{array}{rcl} X(n) & = & \displaystyle\sum_{a=0}^n p_{n,a}\big(1 + X(n-a)\big) \; = \; 1 + \sum_{a=0}^n p_{n,a} X(n-a) \\ & = & \displaystyle1 + \sum_{a=0}^{n-2} p_{n,a}X(n-a) \; = \; 1 + \sum_{a=0}^{n-2} \frac{(a+1)\big[(n-a)! - 2(n-a-1)! + (n-a-2)!\big]}{n!}X(n-a) \\ & = & \displaystyle1 + \sum_{a=2}^n \frac{(n+1-a)[ a! - 2(a-1)! + (a-2)!]}{n!}X(a) \\ & = & \displaystyle1 + \sum_{a=2}^n \frac{(n+1-a)(a^2 - 3a + 3)(a-2)!}{n!}X(a) \end{array} for all n 2 n \ge 2 , so that X ( 2 ) = 2 X(2) = 2 and X ( n ) = n ( n 1 ) 2 n 3 [ 1 + a = 2 n 1 ( n + 1 a ) ( a 2 3 a + 3 ) ( a 2 ) ! n ! X ( a ) ] X(n) \; = \; \dfrac{n(n-1)}{2n-3}\left[1 + \sum_{a=2}^{n-1} \frac{(n+1-a)(a^2 - 3a + 3)(a-2)!}{n!}X(a)\right] for all n 3 n \ge 3 .

A quick computer calculation yields X ( 15 ) = 469087886108518 8538321867075 X(15) \; =\; \dfrac{469087886108518}{8538321867075} so the answer is 469087886108518 + 8538321867075 = 477626207975593 469087886108518 + 8538321867075 = 477626207975593 .

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...