Bubble Sort with "while" loop - why is average case n^2?
Let's prove a more general result:


  Any sorting algorithm which only swaps adjacent elements makes $\Omega(n^2)$ swaps on average.


Let us assume that the elements being sorted are $1,\ldots,n$, and their positions are $i_1,\ldots,i_n$. We need to move element $j$ from position $i_j$ to position $j$. Each swap changes the position of two elements by 1. It follows that the number of swaps is at least
$$
\frac{1}{2} \sum_{j=1}^n |i_j - j|.
$$
The marginal distribution of each $i_j$ is uniform over $\{1,\ldots,n\}$, hence the expected number of swaps is at least
$$
\frac{1}{2n} \sum_{i=1}^n \sum_{j=1}^n |i-j| = \frac{n^2-1}{6}.
$$