The example is given below :
But I do not understand why the variation of $f$ with respect to the given partition is as given? could anyone explain this for me, please?
I have calculated from $x = 1/2n$ to $x = 1/(2n - 4)$ but it seems like I am missing some details in the calculation.

The given variation ($1 + \frac 12 + \cdots \frac 1n$) is correct.
To see this, set $x_i = \frac{1}{2n-i}$ for $i=0,\ldots , 2n-1$.
Then you have $$\cos \frac{\pi}{2x_i} = \cos \frac{\pi}{2}(2n-i)= \cos (n\pi-i\frac{\pi}{2}) = (-1)^n\cos \frac{\pi}{2}i$$
Hence, for $i=2k+1$ ($k=0,\ldots , n-1$) you have $\cos \frac{\pi}{2x_i} = 0$ and for $i=2k$ you have $\cos \frac{\pi}{2x_i} = (-1)^k$
This means for the variation, that only the terms with $i=2k$ contribute. But shifting the index one by one each term with $x_{2k}$ appears twice: $$V(f,P_n) = \underbrace{\left|x_{0}\cos \frac{\pi}{2x_0} - f(0)\right|}_{=1}+ \sum_{i=1}^{2n-1}\left|x_{i+1}\cos x_{i+1} - x_i \cos x_i \right|$$ $$ = \pmb{2}\sum_{k=0}^{n-1}|x_{2k}| = \pmb{2}\sum_{k=0}^{n-1}\frac{1}{2n - 2k} = \sum_{k=0}^{n-1}\frac{1}{n - k}$$