Expectation for # Exchanges (Quicksort Algorithm)

  • Context: Graduate 
  • Thread starter Thread starter rwinston
  • Start date Start date
  • Tags Tags
    Algorithm Expectation
Click For Summary

Discussion Overview

The discussion revolves around the derivation of the expected number of exchanges in the Quicksort algorithm, specifically referencing Hoare's original paper. Participants explore the mathematical reasoning behind the expectation calculation, focusing on the partition process and the summation with respect to the pivot's position.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant questions how Hoare derives the expectation for the number of exchanges, particularly the second equation related to summing with respect to r.
  • Another participant expresses confusion about the meaning of "summing in respect to r" and challenges the use of (N-r-1) instead of (N-r) in the calculations.
  • A different participant clarifies that the expression (N-r-1)/N represents the probability of a number being greater than the pivot and explains the reasoning behind summing from r=1 to N to account for all possible pivot positions.
  • This participant provides a detailed breakdown of the summation process and the resulting formula for the expected number of exchanges, confirming the initial claim made by the first participant.
  • A later reply indicates that the explanation provided by the third participant helped clarify the concept for another participant.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the mathematical derivation, with some finding clarity while others remain confused about specific aspects. No consensus is reached on the interpretation of certain terms or the correctness of the expressions used.

Contextual Notes

Participants highlight potential misunderstandings regarding the summation process and the definitions of terms used in the derivation. Specific mathematical steps and assumptions are not fully resolved.

rwinston
Messages
36
Reaction score
0
Hi

I am reading Hoare's original paper where he derives the complexity of quicksort. I am trying to figure how he derives the expectation for the number of exchanges (sorry if this is a very CS-specific question):

Consider the situation at the end of the partition process, when the bound
was the rth key value in order of magnitude. As a result of the final
exchange, the item which yielded this key value will occupy the rth position
of the segment, and the r- 1 items with lesser key value will occupy the
r- 1 positions below it in the store. The number of exchanges made in the
course of the partition process is equal to the number of items which
originally occupied the r- 1 positions of the lower resulting segment, but
which were removed because they were found to have key values greater
than the bound. The probability of any key value being greater than the
bound is (N- r- I)[N, and therefore the expected number of such items
among the r- 1 items which originally occupied what was to be the lower
resulting segment is"

[tex] \frac{(N-r-1)(r-1)}{N}[/tex]

Summing with respect to r, dividing by N, and adding one for the final
exchange of the item which yielded the bound, we get the absolute
expectation of the number of exchanges:

[tex]\frac{N}{6}+\frac{5}{6N}[/tex]

I can't see how he derives the second equation, or what is involved in summing with respect to r.

Any CS grads out there who can help?
 
Physics news on Phys.org
I didn't understand the same as you. I was working in "suming in respect to r" and trying to figure out what does it mean. I've tryed to sum from r=1 to N, but it doesn't work. And I haven't understand why he uses (N-r-1) and do not use (N-r), what I think is correct.

Did you find the answer for your question? If yes, please put it here or send to me at
leonardoguio@gmail.com

Please, send me the Hoare original paper too.

Thanks.
 
If you understand the number (n-r-1)/n as the propability of a given number to be bigger than the pivot and (r-1) the quantity of these given number that are in the wrong place, it's easy.

Sum in respect to r means that the number above consider that the pivot is in a given position r. But what position is this? The probability of the pivot to be the first number (and r=1) is 1/n, which is the same as the probability to the pivot be in any other position. So, in the middle case, we have to sum (n-r-1).(r-1)/n from 1 to n, which are the possible correct positions of the pivot, and then divide by n, because its a probability. After this, we have to sum 1 to the result, because it is the change of the pivot (to put the pivot in the correct position).

The result will be the number you typed above.

Below, consider thar every sum is from r=1 to n:

(1/n)[tex]\sum[/tex](n-r-1)(r-1) = (1/n)[tex]\sum[/tex](nr-n-r^2+1)= (1/n)[tex]\sum[/tex]nr - (1/n)[tex]\sum[/tex]n - (1/n)[tex]\sum[/tex](r^2) + (1/n)[tex]\sum[/tex]1 =(n+1)n/2 - n - (n+1)(2n+1)/6 + 1 = (n^2)/6 + (5/6) - n

Divide by n (probability) and sum 1 to the result (the exchange to put the pivot in the correct position) and we get:
n/6 + 5/6n

:biggrin:
 
Thanks Leonardo! That makes sense now :-)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
895
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K