Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(Search): add Jump Search optimized version (works for descending…#1844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
jaimin45-art wants to merge2 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromjaimin45-art:feat/jump-search-optimized
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletionsMaths/MobiusFunction.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,6 @@ export const mobiusFunction = (number) => {
return primeFactorsArray.length !== new Set(primeFactorsArray).size
? 0
: primeFactorsArray.length % 2 === 0
? 1
: -1
? 1
: -1
}
59 changes: 59 additions & 0 deletionsSearch/JumpSearchOptimized.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
/**
* This version of Jump Search works for both ascending and descending sorted arrays.
*
* Time Complexity: O(√n)
* Space Complexity: O(1)
*
* Example:
* jumpSearchOptimized([1, 3, 5, 7, 9, 11], 7) -> 3
* jumpSearchOptimized([20, 15, 10, 5, 0], 10) -> 2
*/

function jumpSearchOptimized(arr, target) {
if (!Array.isArray(arr) || arr.length === 0) return -1

const n = arr.length
const step = Math.floor(Math.sqrt(n))
let prev = 0

// Detect array order
const isAscending = arr[0] < arr[n - 1]

// Jump in blocks based on order
while (prev < n) {
const next = Math.min(prev + step, n)
const value = arr[next - 1]

if ((isAscending && value >= target) || (!isAscending && value <= target)) {
// Linear search in the found block
for (let i = prev; i < next; i++) {
if (arr[i] === target) return i
}
return -1
}

prev = next
}

return -1
}

module.exports = { jumpSearchOptimized }

/* -----------------------------------------
Quick local test: run `node Search/JumpSearchOptimized.js`
----------------------------------------- */
if (require.main === module) {
const tests = [
{ arr: [1, 3, 5, 7, 9, 11], target: 7 },
{ arr: [20, 15, 10, 5, 0], target: 10 },
{ arr: [2, 4, 6, 8, 10, 12], target: 11 },
{ arr: [], target: 3 }
]

tests.forEach(({ arr, target }) => {
console.log(
`Array: [${arr}] | Target: ${target} | Index: ${jumpSearchOptimized(arr, target)}`
)
})
}
37 changes: 37 additions & 0 deletionsSearch/test/JumpSearchOptimized.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
import { jumpSearchOptimized } from '../JumpSearchOptimized'

test('jumpSearchOptimized([0, 0, 4, 7, 10, 23, 34, 40, 55, 68, 77, 90], 77) => 10', () => {
const arr = [0, 0, 4, 7, 10, 23, 34, 40, 55, 68, 77, 90]
const res = jumpSearchOptimized(arr, 77)
expect(res).toEqual(10)
})

test('jumpSearchOptimized([11, 12, 15, 65, 78, 90], 4) => -1', () => {
const arr = [11, 12, 15, 65, 78, 90]
const res = jumpSearchOptimized(arr, 4)
expect(res).toEqual(-1)
})

test('jumpSearchOptimized([11, 12, 15, 65, 78, 90], 11) => 0', () => {
const arr = [11, 12, 15, 65, 78, 90]
const res = jumpSearchOptimized(arr, 11)
expect(res).toEqual(0)
})

test('jumpSearchOptimized([], 50) => -1', () => {
const arr = []
const res = jumpSearchOptimized(arr, 50)
expect(res).toEqual(-1)
})

test('jumpSearchOptimized([5, 10, 15, 20, 25], 25) => 4', () => {
const arr = [5, 10, 15, 20, 25]
const res = jumpSearchOptimized(arr, 25)
expect(res).toEqual(4)
})

test('jumpSearchOptimized([1, 3, 5, 7, 9], 2) => -1', () => {
const arr = [1, 3, 5, 7, 9]
const res = jumpSearchOptimized(arr, 2)
expect(res).toEqual(-1)
})
Loading

[8]ページ先頭

©2009-2025 Movatter.jp