codinasion-archive / codinasion-monorepo

Community Monorepo
https://codinasion.org
MIT License
52 stars 167 forks source link

Write a R program to implement jump search #5269

Open harshraj8843 opened 10 months ago

harshraj8843 commented 10 months ago

Description

Write a R program to implement jump search

Like Binary Search, Jump Search is a searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some elements in place of searching all elements.

For example, suppose we have an array arr[] of size n and block (to be jumped) size m. Then we search at the indexes arr[0], arr[m], arr[2m]…..arr[km] and so on. Once we find the interval (arr[km] < x < arr[(k+1)m]), we perform a linear search operation from the index km to find the element x.

Pseudocode

procedure jump_search
   A ← sorted array
   n ← size of array
   x ← value to be searched

   Set block size = √n

   while A[min(block size, n)-1] < x do
      prev = block size
      block size = block size + √n
      if prev >= n
         return not found

   end while

   while A[prev] < x do
      prev = prev + 1

      if prev == min(block size, n)
         return not found

   end while

   if A[prev] == x
      return prev

   return not found
end procedure

Example

list = [1,2,3,4,5]
value = 4

Output : 3
How to contribute - Comment `!assign` to assign this issue to yourself - Fork this repository - Create a new branch - Save the solution in `program/program/implement-jump-search/implement_jump_search.r` - Commit the changes - Create a pull request
codinasion-bot[bot] commented 10 months ago

👋🏻 Hey @harshraj8843

💖 Thanks for opening this issue 💖

A team member should be by to give feedback soon.

srini696 commented 9 months ago

!assign