Random Merge Sort

In the book of algorithms, I was asked the following question:

Suppose merge sorting is implemented to split a file into a random position rather than in the middle. How many comparisons will this method use to sort n items on average?

Thank.

+5
source share
3 answers

To give you an answer, consider these more specific questions:

Suppose the split is always 10%, or 25%, or 75%, or 90%. In each case: what is the effect on the depth of the recursion? How many comparisons should be at the recursion level?

+2
source

I partially agree with @Armen, they should be comparable.

: , . n, 2*n - 1 ( , ), . log2(n) , n*log2(n) .

: , n1 n2, n1 + n2 - 1. Howerer, , 1 n-1 n/2. , , .

, , , n max(n1, n2) n/2. max(n1, n2) 3*n/4,

n*log43(n) // where log43 is log in base 4/3

n * log2(n) / log2(4/3) ~= 2.4 * n * log2(n)

- , , , , . , ,

0

2n * H_ {n - 1} <= 2n ln n, , n n . quicksort (. http://www.cs.cmu.edu/afs/cs/academic/class/15451-s07/www/lecture_notes/lect0123.pdf).

, n 2 L R. R L, L R. , , , , n, .

, ? " ", R L ( ). , j- i- , 1/(j - i), j > i. , , j , , " " {i + 1,..., j}. j , , , {i + 1,..., j}, j , j - , .

Thus, the expected total number of comparisons with j is not more than H_n (i.e. 1 + 1/2 + 1/3 ..., where the number of members is not more than n - 1). Summing over all possible j gives n * H_ {n - 1}. This only allowed for a right-to-left comparison, so the final upper bound is 2n * H_ {n - 1}.

0
source

All Articles