User:TheEternalVortex
From Wikipedia, the free encyclopedia
For the analysis we will count compares and arithmetic operations. There is no multiplication/division used so they should all be roughly the same time.
Sequential search does 2 compares per iteration and one increment per iteration. When searching for an item in the array we expect n/2 iterations, and n iterations if searching for an item not in the array. Thus the expected number of compares+increments is E(C) = 3(0.1(n) + 0.9(n / 2)) = 0.3n + 1.35n = 1.65n.
Binary search always does lg n iterations (in this implementation). Each iteration it does 2 compares, 1 subtract, 1 add, 1 bitshift, and 1 assignment, for 6 total operations, regardless of whether the item is found or not. So we get 6 lg n. Binary search also does 2 assignments and 2 compares and 1 or no matter what n, so we actually have 5 + 6 lg n.
Empirically about n = 50, numerically ~18.