MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Jump2

Function Jump2

search/jump2.go:5–24  ·  view source on GitHub ↗
(arr []int, target int)

Source from the content-addressed store, hash-verified

3import "math"
4
5func Jump2(arr []int, target int) (int, error) {
6 step := int(math.Round(math.Sqrt(float64(len(arr)))))
7 rbound := len(arr)
8 for i := step; i < len(arr); i += step {
9 if arr[i] > target {
10 rbound = i
11 break
12 }
13 }
14
15 for i := rbound - step; i < rbound; i++ {
16 if arr[i] == target {
17 return i, nil
18 }
19 if arr[i] > target {
20 break
21 }
22 }
23 return -1, ErrNotFound
24}

Callers 2

TestJump2Function · 0.85
BenchmarkJump2Function · 0.85

Calls

no outgoing calls

Tested by 2

TestJump2Function · 0.68
BenchmarkJump2Function · 0.68