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

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.