A loop that halves
transcript
Take the number forty. Cut it in half. Again and again. How many cuts until one? Count them by running.
Forty becomes twenty. Twenty becomes ten, then five. Five becomes two. Two and a half rounds down. Two becomes one. Five cuts in all.
Double the input and see. A thousand takes nine cuts. Two thousand takes ten. Four thousand takes eleven. Doubling adds exactly one cut.
A million items take nineteen cuts. A billion items take only twenty nine. Halving loops stay tiny at any size. That is the power of cutting in half.
Try thirty three. Thirty three cuts to sixteen. Rounded down. Sixteen, eight, four, two, one. That is five cuts, not six. The count is floor of log base two.
Cheap loops make big inputs easy. A billion items need only twenty nine passes. Later you will meet this loop again. Binary search is one of them.
Halve it. Count the cuts. Full breakdown in the lecture.