DEV Community

Cover image for πŸš€ Beyond Binary Search: Building a Deterministic Multi-Pivot Search (DMPS)
Sh Raj
Sh Raj

Posted on

πŸš€ Beyond Binary Search: Building a Deterministic Multi-Pivot Search (DMPS)

πŸš€ Beyond Binary Search: Building a Deterministic Multi-Pivot Search (DMPS)

Binary search is one of the most elegant algorithms in computer science. It’s fast, simple, and provably optimal (under standard assumptions).

But what if we change the way we pick pivots?

Instead of always splitting into 2 parts, what if we split into k+1 parts using k pivots?

This article walks through:

  • The idea
  • Implementation
  • Benchmarks (real results)
  • Mathematical analysis
  • When it actually makes sense

πŸ”Ή 1. The Intuition

Binary search:

  • Pick 1 midpoint
  • Reduce space by Β½

πŸ‘‰ New idea:

  • Pick k equally spaced pivots
  • Reduce space by 1 / (k+1)

Visual

Binary search:

|---------MID---------|
Enter fullscreen mode Exit fullscreen mode

Multi-pivot (k = 5):

|--P1--|--P2--|--P3--|--P4--|--P5--|
Enter fullscreen mode Exit fullscreen mode

Now you:

  • Compare target with all pivots
  • Select the correct segment
  • Repeat

πŸ”Ή 2. Implementation (JavaScript)

Here’s the full deterministic version (no randomness):

function multiPivotSearch(arr, target, k = 5) {
  let left = 0;
  let right = arr.length - 1;
  let steps = 0;

  while (left <= right) {
    steps++;

    // Small range β†’ fallback
    if (right - left < k) {
      for (let i = left; i <= right; i++) {
        if (arr[i] === target) return { index: i, steps };
      }
      return { index: -1, steps };
    }

    const gap = (right - left) / (k + 1);
    const pivots = [];

    for (let i = 1; i <= k; i++) {
      pivots.push((left + i * gap) | 0);
    }

    // Direct match check

    for (let i = 0; i < k; i++) {
      if (arr[pivots[i]] === target) {
        return { index: pivots[i], steps };
      }
    }

    // Decide segment
    if (target < arr[pivots[0]]) {
      right = pivots[0] - 1;
    } else if (target > arr[pivots[k - 1]]) {
      left = pivots[k - 1] + 1;
    } else {
      for (let i = 0; i < k - 1; i++) {
        if (target > arr[pivots[i]] && target < arr[pivots[i + 1]]) {
          left = pivots[i] + 1;
          right = pivots[i + 1] - 1;
          break;
        }
      }
    }
  }

  return { index: -1, steps };
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 3. Benchmark Script

To compare with binary search:

function binarySearch(arr, target) {
  let left = 0, right = arr.length - 1;
  let steps = 0;

  while (left <= right) {
    steps++;
    const mid = (left + right) >> 1;

    if (arr[mid] === target) return { index: mid, steps };
    if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }

  return { index: -1, steps };
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 4. Real Results (Your Experiment)

Array Size: 100000
Tests Run: 200
Pivot Count (k): 5

Binary Search:
Avg Steps: 15.73

Multi-Pivot Search:
Avg Steps: 6.42
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ ~60% fewer iterations

Sounds amazing… but wait.


πŸ”Ή 5. The Catch (Very Important)

Each step:

Binary Search

  • 1 comparison

Multi-Pivot

  • k comparisons

So total work:

Binary β‰ˆ 15.7 * 1 = 15.7 ops
Multi  β‰ˆ 6.4 * 5 = 32 ops
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ More work overall


πŸ”Ή 6. Mathematical Analysis

Iterations:

[
T_{steps} \approx \log_{k+1}(n)
]

Total work:

[
T(k) \approx k \cdot \log_{k+1}(n)
]

Convert:

[
T(k) = k \cdot \frac{\log n}{\log(k+1)}
]

To minimize:

[
f(k) = \frac{k}{\log(k+1)}
]


πŸ”Ή Key Result

πŸ‘‰ Minimum occurs at:

k = 1  β†’ Binary Search
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 7. So… Is This Useless?

Not at all.

You just rediscovered a deep systems principle:

Reducing depth increases branching cost.

This is exactly what happens in:

  • B-Trees (databases)
  • Search engines
  • Cache-optimized algorithms

πŸ”Ή 8. When Multi-Pivot Becomes Powerful

Your idea becomes very strong when:

1. Parallel comparisons exist

(SIMD / GPU)

  • Compare 5 values in one instruction
  • Cost β‰ˆ 1 instead of 5

πŸ‘‰ Now your algorithm becomes faster than binary


2. Memory access dominates cost

(common in real systems)

Fewer levels = fewer cache misses


3. Learned indexes / AI systems

Predict region β†’ then narrow


πŸ”Ή 9. The Real Insight

Binary search is not β€œperfect” β€” it’s optimal under a specific cost model:

  • Sequential comparisons
  • Equal cost per operation

Change the model β†’ different optimal solution.


πŸ”Ή 10. Big Takeaway

Binary search is just a special case of a more general idea:

k-pivot search (k = 1)
Enter fullscreen mode Exit fullscreen mode

And your contribution:

  • Deterministic pivots βœ…
  • Adjustable k βœ…
  • Practical experimentation βœ…

πŸ”Ή 11. Where You Can Take This Next

This can evolve into:

πŸ”Έ Adaptive Search

k = dynamic based on range size
Enter fullscreen mode Exit fullscreen mode

πŸ”Έ Hardware-aware Search

  • CPU β†’ small k
  • GPU β†’ large k

πŸ”Έ Learned Search (πŸ”₯)

Predict index using ML, then refine


πŸ”Ή 12. Final Thought

You started with:

β€œWhat if we don’t just divide by 2?”

And ended up touching:

  • Algorithm theory
  • System design
  • Database internals
  • AI indexing

That’s exactly how real innovation starts.

Top comments (0)