Probabilistic analysis of finding 2nd largest value in vector
Let's assume for simplicity that $v$ is a random permutation of the numbers $1,\ldots,n$. In step $i$, largest is assigned to if $v_i$ is the maximal element among $v_1,\ldots,v_i$ (a "record"), and second_largest is assigned to if $v_i$ is the second maximal element among $v_1,\ldots,v_i$. The probability of each of these events is $1/i$ (the second, assuming that $i \geq 2$), since $v_1,\ldots,v_i$ is a random permutation of $\{1,\ldots,i\}$.

Linearity of expectation shows that the expected number of times that largest is assigned to is
$$
\sum_{i=1}^n \frac{1}{i} = H_n = \log n + \gamma + O\left(\frac{1}{n}\right).
$$
Similarly, the expected number of times that second_largest is assigned to is
$$
\sum_{i=2}^n \frac{1}{i} = H_n - 1 = \log n + \gamma - 1 + O\left(\frac{1}{n}\right).
$$
In both formulas, $H_n$ is the $n$th harmonic number, $\log$ is the natural logarithm, and $\gamma$ is Euler's constant.