Skip to content

Loading bars and probabilities

Published:

It showed up like a random, innocent question from a friend of mine while we were just hanging out in a Discord voice chat. Suppose there is a single element we are looking for in an unsorted array of nn elements. We would like the user to have some kind of loading bar so that, with high probability, the loading bar is full exactly as the search is complete. What kind of relationship should exist between the loading bar and the search?

The intuition some may have is that most of the time you will find the element before iterating over the whole array, so the bar should reach 100% earlier. But is that true? And if so, what is the actual math behind it?

Let’s start by the basics. Assuming the element has an equal probability of being in any position in the array, the probability of it being the first element is P(En1)=1nP(E^1_n) = \frac{1}{n}. Therefore, the probability of it not being said element is P(¬En1)=1P(En1)=11n=n1nP(\neg E^1_n) = 1 - P(E^1_n) = 1 - \frac{1}{n} = \frac{n - 1}{n}.
If we are exploring the second element of the list, we must assume that the first element is not the one we are looking for. So the probability becomes P(En2)=P(En11¬En1)=P(En11¬En1)P(¬En1)=1n1n1n=1nP(E^2_n) = P(E^1_{n - 1} \wedge \neg E^1_n) = P(E^1_{n - 1} | \neg E^1_n) \cdot P(\neg E^1_n) = \frac{1}{n - 1} \cdot \frac{n - 1}{n} = \frac{1}{n}. In other words, we are back to the original probability.

So, every time we increment the index of the element we are analysing, we have a 1n\frac{1}{n} chance of finding the one we are looking for. This suggests that the loading bar should be incremented by 1n\frac{1}{n} every time we increment the index of the element we are analysing, creating a 1:1 relationship between the search and the loading bar.

While this seems to be correct from a mathematical point of view, it may not be the right answer for the user experience. While talking about this issue with a colleague, he suggested that the loading bar should increase rapidly at the beginning and slow down as the search progresses. This is because the user will be enticed by the promise of a fast operation, all thanks to the immediate feedback. Even if they are unlucky and the element is at the end of the array, the loading bar will be almost full, and by the fallacy of sunk costs, they will be more willing to wait for the search to complete.
An interesting mixture of math and psychology.

Summary

P(En1)=1nP(¬En1)=1P(En1)=11n=n1nP(En2)=P(En11¬En1)=P(En11¬En1)P(¬En1)=1n1n1n=1n\begin{array}{ll} P(E^1_n) &&&= \frac{1}{n} \newline P(\neg E^1_n) &= 1 - P(E^1_n) &= 1 - \frac{1}{n} &= \frac{n - 1}{n} \newline P(E^2_n) &= P(E^1_{n - 1} \wedge \neg E^1_n) = P(E^1_{n - 1} | \neg E^1_n) \cdot P(\neg E^1_n) &= \frac{1}{n - 1} \cdot \frac{n - 1}{n} &= \frac{1}{n} \end{array}