Proofs: Jump search
These are the human proofs of Jump search, at full length.
Jump search returns the first index of x, or NIL when x is not in A full
This proof shows: correctness, termination.
Let be sorted in non-decreasing order, , and let be a key. Let be the lower bound of . Because is sorted, for and for . We prove that JUMP-SEARCH stops and returns when and , and NIL otherwise. This is the smallest index of when is present, since the copies of are exactly the indices with , and they start at when there are any.
Invariant of the jump loop. Before each test of line L3:
- and , where is the value of line L1.
- for each , that is, .
- or .
Start. Line L2 sets and . Then , , statement 2 is empty, and statement 3 holds.
Step. The body runs only when and . Since is sorted, for each . Line L4 sets and . Then statement 2 holds for . Also gives and , and is or .
Exit. The loop stops when or . In the first case . In the second case and , so . In both cases, with statement 2,
Termination of the jump loop. Each pass makes , since and . So grows by at least 1 in each pass and never passes , and there are at most passes. More exactly, while , statement 3 gives , so grows by in each pass but the last, and there are at most passes.
The scan. Line L5 sets . Before each test of line L6, and . This holds at the start by the exit statement. A pass runs only when and , which means ; so the new satisfies and . Each pass increases by 1 and , so the scan stops. When it stops, either , and then ; or with , and then by the definition of . In both cases .
Result. Line L8 runs with . If and , it returns , the smallest index of . Otherwise is absent: if then every item is less than , and if then every has and every has . So line L9 returns NIL correctly.
The number of comparisons. The jump loop tests only while , and by the termination argument this happens at most times. The scan compares at most items. Line L8 makes at most one comparison. So the total is at most .