Suppose each [1] represents a standing person that carries a value (in this case, everybody carries 1).
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1]
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1]
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1]
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1]
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1]
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1]
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1]
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1]
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1]
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1]
How would you suggest counting the number of these people? Well, one way to solve this problem is to stop by each person and ask him/her about the value he/she carries and add these values together. For example, we would add
1 + 1 + ... + 1 for each person in the room
If there are n persons in the room, this would take us exactly n steps. This algorithm has a linear run time because as the number of people in the room grows larger, the number of steps we take will grow linearly larger. For example, if the there were n + 10 persons in the room, we would take n + 10 steps to count them.
Fortunately, there's a faster algorithm in the sense that we can take less number of steps. This algorithm is as follows
- let exactly half the number of persons in the room leave.
- for each person that leaves, have his/her value added to a stayed person.
- repeat until you only have one person left.
Let's visualize this algorithm
Step 1:
[2] [2] | [2] [2] | [2] [2] | [2] [2] | [2] [2]
-----------------------------------------------
[2] [2] | [2] [2] | [2] [2] | [2] [2] | [2] [2]
-----------------------------------------------
[2] [2] | [2] [2] | [2] [2] | [2] [2] | [2] [2]
-----------------------------------------------
[2] [2] | [2] [2] | [2] [2] | [2] [2] | [2] [2]
-----------------------------------------------
[2] [2] | [2] [2] | [2] [2] | [2] [2] | [2] [2]
Step 2:
[4] [4] | [4] [4] | [4] [4]
---------------------------
[4] [4] | [4] [4] | [4] [4]
---------------------------
[4] [4] | [4] [4] | [4] [4]
---------------------------
[4] [4] | [4] [4] | [4] [4]
---------------------------
[4]
Step 3:
[8] [8] | [8] [8] | [8] [8]
---------------------------
[8] [8] | [8] [8] | [8] [12]
/* NOTICE that we added the values of 2 leaving person to one stayed person
* BECAUSE we cannot have 1/2 a person leave */
Step 4:
[16] [16] | [16] [16] | [16] [20]
Step 5:
[32] [32] | [36]
/* NOTICE that we added the values of 2 leaving person to one stayed person
* BECAUSE we cannot have 1/2 a person leave */
Step 6:
[100]
So it took us exactly 6 steps to count the number of people. The run time of this algorithm is logarithmic (exactly log base 2 of n where n is the size of input). Because as the size of input doubles, we take only one extra step to count the number of people. So if we had 200 persons in the room, we would take 7 steps. You can try that!