Finding the average time complexity for a max algorithm I'm trying to find the average-case number of times that max is assigned a value by the below algorithm find_max.
find_max(L):
     max = -oo # negative infinity
     for k = 0 to len(L)-1:
          if L[k] &gt; max:
               max = L[k]
     return max

However, I'm struggling to come to a solution.  So far, this is what I have done:
I'm defining the sample space to be L containing values which are between 1 and 100, and having them uniformly distributed.
The worst-case running time is $T(n) = n + 1$ because of the initial max assignment, and the case where every element is larger than the previous one.
The best-case running time is $T(n) = 2$ which is the case where all elements have the same value, so there is the initial max assignment and one assignment in the for loop.
So, the running time is distributed between 2 and n+1 inclusive.
Then, the average-case running time is:
$E[t_{n}] = \sum_{2}^{n+1} t \cdot P(t)$
However, I am having difficulty defining the Random Variable T and the probability formula for values of T.
Let $\small n = \mathsf{len}(L)$ and $\small t_n$ be the times that $\small \mathsf{max}$ is assigned a value. For $\small 0 \leq i &lt; n$, define a random variable $\small X_i$ as
$$
\small
X_i = \begin{cases}
1 &amp; \text{if } L[i] &gt; \text{$\mathsf{max}$ in the $i$-th iteration of the algorithm}\\
0 &amp; \text{otherwise}
\end{cases}
$$
Then we have
$$
\small
t_n = 1 + \sum_{i=0}^{n-1} X_i
$$ By linearity of expectation,
$$
\small
\mathbb{E}[t_n] = 1 + \sum_{i=0}^{n-1}\mathbb{E}[X_i]
$$
Observe that $\small X_i = 1$ if and only if $\small L[i] &gt; \max\{L[0], L[1], \cdots, L[i-1]\}$. Therefore
\begin{align}
\small
\mathbb{E}[X_i] = \Pr[X_i = 1]  =&amp;\small \sum_{v=1}^{100} \Pr[L[0] &lt; v, L[1] &lt; v, \cdots, L[i-1] &lt; v, L[i] = v] \\
\small= &amp;\small \sum_{v=1}^{100} \left(\frac{v-1}{100}\right)^i \cdot \frac{1}{100}
\end{align}
We thus conclude
\begin{align}
\small
\mathbb{E}[t_n] =&amp;\ \small  1 + \sum_{i=0}^{n-1}\sum_{v=1}^{100} \left(\frac{v-1}{100}\right)^i \cdot\frac{1}{100} \\
\small =&amp;\ \small 1 + \frac{1}{100}\sum_{v=1}^{100}\sum_{i=0}^{n-1}\left(\frac{v-1}{100}\right)^i \\
\small =&amp;\ \small 1 + \frac{1}{100}\sum_{v=1}^{100}\frac{1 - \left(\frac{v-1}{100}\right)^n}{1 - \frac{v-1}{100}} \\
\small\text{(for sufficiently large $n$)}\quad\quad\approx&amp;\ \small1 + \frac{1}{100}\sum_{v=1}^{100} \frac{1}{1 - \frac{v-1}{100}} \\
\small = &amp;\ \small 1 + \sum_{v=1}^{100}\frac{1}{101-v} \\
\small =&amp;\ \small 1 + H_{100}
\end{align}
where $\small H_{100}$ is the $\small 100$-th harmonic number.