I'm trying to understand the average run time of quicksort which is $O(n \log n)$.
I understand the intuition behind it: if we partition array $A$ to e.g. $\alpha n $ and $(1-\alpha)n$ then we essentially partition $\log n$ times and each partition performs $n$ comparisons. And this happens with high probability; e.g. partition to $c$ and $n-c$ for a constant $c$ is less likely because $c/n$ is smaller than $\alpha n/n$ for large $n$.
Now I want to prove this bound. The first observation is that if we consider a sorted array $A'$, then elements $A'[i]$ and $A'[j]$ are compared at most once. This is because we only compare with pivot and then remove it during partitioning. Therefore the expected number of comparisons becomes
\begin{align} \mathbb{E}\left[\sum_{i=1}^n \sum_{j=i+1}^n 1\{A'[i] \text{ compared with } A'[j]\} \right] = \sum_{i=1}^n \sum_{j=i+1}^n P\{A'[i] \text{ compared with } A'[j]\} \end{align}
It remains to compute the probability of comparing $A'[i]$ and $A'[j]$. Now one argument is that if we pick a pivot before $A'[i]$ or after $A'[j]$, then $A'[i]$ and $A'[j]$ remain together in the next subarray. So this probability becomes the probability of choosing $A'[i]$ or $A'[j]$ as a pivot in subarray $A'[i,...,j]$ which is simply $2/(j-i+1)$. My question is: Why do we only need to compute the probability of choosing $A'[i]$ or $A'[j]$ as a pivot only in subarray $A'[i,...,j]$?
I'm confused because I think at the beginning we have an array of length $n$ so there is a $2/n$ chance for picking them as a pivot, some chance $x_1$ to pick a pivot between them (so they'll never be compared), and some chance $1-x_1-2/n$ to pick a pivot before or after them (so they remain in the same subarray and may be computed). In the last case, again the probability of picking one $A'[i]$ and $A'[j]$ depends on the array length. How can I show that this line of thinking also gives the same runtime?
At the same time, it makes intuitive sense that the probability of comparison depends only on the number of elements between $A'[i]$ and $A'[j]$.