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 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 . Therefore, the probability of it not being said element is .
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 .
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 chance of finding the one we are looking for. This suggests that the loading bar should be incremented by 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.