Quiz Comment Box

search

Jump search technique

Jump search techniquealso works for ordered lists. It creates a block and tries to find the element in that block. If the item is not in the block, it shifts the entire block. The block size is based on the size of the list. If the size of the list is n then block size will be √n. After finding a correct block it finds the item using a linear search technique. The jump search lies between linear search and binary search according to its performance.

Example :

Following are the implementation of Jump search into programs

                        
   // C program for jhmup Search
    #include
        #include
        
        using namespace std;
        int jumpSearch(int array[], int size, int key) {
           int start = 0;
           int end = sqrt(size); //the square root of array length
        
           while(array[end] <= key && end < size) {
              start = end; //it it is not correct block then shift block
              end += sqrt(size);
              if(end > size - 1)
                 end = size; //if right exceeds then bound the range
           }
        
           for(int i = start; i> n;
           int arr[n]; //create an array of size n
           cout << "Enter items: " << endl;
        
           for(int i = 0; i< n; i++) {
              cin >> arr[i];
           }
        
           cout << "Enter search key to search in the list: ";
           cin >> searchKey;
           if((loc = jumpSearch(arr, n, searchKey)) >= 0)
              cout << "Item found at location: " << loc << endl;
           else
              cout << "Item is not found in the list." << endl;
        }
                               
                        
                         Code 
                    
                        
   // Java program to implement Jump Search.
    public class JumpSearch
    {
        public static int jumpSearch(int[] arr, int x)
        {
            int n = arr.length;
     
            // Finding block size to be jumped
            int step = (int)Math.floor(Math.sqrt(n));
     
            // Finding the block where element is
            // present (if it is present)
            int prev = 0;
            while (arr[Math.min(step, n)-1] < x)
            {
                prev = step;
                step += (int)Math.floor(Math.sqrt(n));
                if (prev >= n)
                    return -1;
            }
     
            // Doing a linear search for x in block
            // beginning with prev.
            while (arr[prev] < x)
            {
                prev++;
     
                // If we reached next block or end of
                // array, element is not present.
                if (prev == Math.min(step, n))
                    return -1;
            }
     
            // If element is found
            if (arr[prev] == x)
                return prev;
     
            return -1;
        }
     
        // Driver program to test function
        public static void main(String [ ] args)
        {
            int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21,
                        34, 55, 89, 144, 233, 377, 610};
            int x = 55;
     
            // Find the index of 'x' using Jump Search
            int index = jumpSearch(arr, x);
     
            // Print the index where 'x' is located
            System.out.println("\nNumber " + x +
                                " is at index " + index);
        }
    }
                        
                         Code 
                    
                        
   # Python3 code to implement Jump Search
import math
 
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[int(min(step, n)-1)] < x:
        prev = step
        step += math.sqrt(n)
        if prev >= n:
            return -1
     
    # Doing a linear search for x in
    # block beginning with prev.
    while arr[int(prev)] < x:
        prev += 1
         
        # If we reached next block or end
        # of array, element is not present.
        if prev == min(step, n):
            return -1
     
    # If element is found
    if arr[int(prev)] == x:
        return prev
     
    return -1
 
# Driver code to test function
arr = [ 0, 1, 1, 2, 3, 5, 8, 13, 21,
    34, 55, 89, 144, 233, 377, 610 ]
x = 55
n = len(arr)
 
# Find the index of 'x' using Jump Search
index = jumpSearch(arr, x, n)
 
# Print the index where 'x' is located
print("Number" , x, "is at index" ,"%.0f"%index)
 



                        
                         Code 
                    

Sample Output

Input: A sorted list of data: 10 13 15 26 28 50 56 88 94 127 159 356 480 567 689 699 780 850 956 995 The search key 356

Output: Item found at location: 11

Visualized

Note: For better Visualized Understanding visit our algorithm Visualizer

Visualize
previous

Sublist Search