Searching Algorithms

How Jump Search Works

158
index 016 of 16 still possible15
  • Ruled out
  • Still possible
  • Checked now
  • Found
1/7

Looking for 158 in 16 sorted values, from 4 to 196.

Settings

More options
Is it in the array?
Gaps between values

Two to 64 numbers. Sorted ascending before the search runs.

Jump Search

  1. 1step = floor(sqrt(n))
  2. 2// jump until the block may hold it
  3. 3while a[min(step, n) - 1] < target:
  4. 4 prev = step; step += floor(sqrt(n))
  5. 5
  6. 6// then walk the block
  7. 7for i in prev..min(step, n)-1:
  8. 8 if a[i] == target: return i
  9. 9return NOT_FOUND

How the Six Compare

AlgorithmBestAverageWorstNeeds a sorted array
LinearO(1)O(n)O(n)No
BinaryO(1)O(log n)O(log n)Yes
TernaryO(1)O(log n)O(log n)Yes
JumpO(1)O(√n)O(√n)Yes
ExponentialO(1)O(log n)O(log n)Yes
InterpolationO(1)O(log log n)*O(n)Yes

Scroll the table sideways for the rest of the columns.

Interpolation search only reaches that average when the gaps between the values are even.