algorithm - how many swap quicksort take to sort data in avg case? -


I am confused between two replies of stakoverflow,

My question is how many swap quickest sort Do the average case data?

This is saying some links (1/3) swap on nln (n) average

Other links suggest That 1 * n * ln (n) swaps on average

I want to know who is right?

Derived from your last link says:

We believe During that one running the average number of swaps is 1/2 ∙ (N-1), it means that only one half of the average elements are swapped.

First of all, it seems that they say the number of swaps, the number of writing instead of the exchanges. Therefore, considering the assumption of their, the total number of exchanges (1/2) should be ∙ N ∙ LN (N). Secondly, I do not think this assumption is correct (this would be if the pivot was always mediocre, instead of being randomly chosen.)

array {x 1 , X < Sub> 2 , ..., x n } is a random permutation of {1, 2, ..., n}, and z is a randomly selected axis quartort The number of exchanges during the first run of the algorithm is the number of indexes I {1, ..., n}, such as i & lt; = Z and x i z is therefore the estimated value of that number:

Σ of = 1 ... n (p (z = k ) ∙ Σ 1 & leq; i & leq; k (p sub> 1) k)) = (1 / n) ∙ Σ of = 1 ... n (Σ 1 & leq; i & leq; k (nk) / n)) = (1 / n) ∙ Σ of = 1 .. .n (k ∙ (nk) / n) = = (1/6) (N-1) (N + 1) / N-4/6 / /

And since it's iterative The average number is 2 ∙ ln (n), the total number of exchanges (1/3) is ∙ n ∙ ln (n).


Comments