Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Jump Search Julia program added (#5722)
Browse files Browse the repository at this point in the history
Co-authored-by: Riyazul555 <riyazulislam2003@gmail.com>
Co-authored-by: Ritesh Kokam <61982298+RiteshK-611@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 15, 2024
1 parent 4c996c2 commit 249a69a
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions program/program/implement-jump-search/implement-jump-search.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function jump_search(arr::Vector{T}, x::T) where T
n = length(arr)
step = floor(Int, sqrt(n))
prev = 0

# Finding the block where element is present
while arr[min(step, n)-1] < x
prev = step
step += floor(Int, sqrt(n))
if prev >= n
return -1 # Element not found
end
end

# Linear search for x in block beginning with prev
while arr[prev] < x
prev += 1

# If we reached the next block or end of array, element is not present.
if prev == min(step, n)
return -1 # Element not found
end
end

# If element is found
if arr[prev] == x
return prev
end

return -1 # Element not found
end

# Example usage
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x = 7

result = jump_search(arr, x)
if result != -1
println("Element $x is at index $result")
else
println("Element $x is not present in the array")
end

0 comments on commit 249a69a

Please sign in to comment.