jump_search.rb
def jumpSearch(arr, x , n)
  # Finding block size to be jumped
  step = Math.sqrt(n)

  # Finding the block where element is
  # present (if it is present)
  prev = 0
  while arr[([step, n].min) - 1] < x
    prev = step
    step += Math.sqrt(n)
    if prev >= n then
      return -1
    end
  end

  # Doing a linear search for x in block beginning with prev.
  while arr[prev] < x
    prev += 1

    # If we reached next block or end of array, element is not present.
    if prev == [step, n].min then
      return -1
    end

    # If element is found
    if arr[prev] == x then
      return prev
    end
  end
end
# Ruby code to implement Jump Search
Ruby code to implement Jump Search
Ruby code to implement Jump Search
def jumpSearch(arr, x , n)
  step = Math.sqrt(n)
  prev = 0
  while arr[([step, n].min) - 1] < x
    prev = step
    step += Math.sqrt(n)
    if prev >= n then
      return -1
    end
  end
  while arr[prev] < x
    prev += 1
    if prev == [step, n].min then
      return -1
    end
    if arr[prev] == x then
      return prev
    end
  end
end
def jumpSearch ( arr , x , n ) step = Math . sqrt ( n ) prev = 0 while arr [ ( [ step , n ] . min ) - 1 ] < x prev = step step += Math . sqrt ( n ) if prev >= n then return - 1 end end while arr [ prev ] < x prev += 1 if prev == [ step , n ] . min then return - 1 end if arr [ prev ] == x then return prev end end end
Ruby code to implement Jump Search
Ruby code to implement Jump Search
# Finding block size to be jumped # Finding the block where element is # present (if it is present) # Doing a linear search for x in block beginning with prev. # If we reached next block or end of array, element is not present. # If element is found
arr
x
n