Bitcoin

Bitcoin
Bitcoin

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.
Let’s consider the following array: (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610). Length of the array is 16. Jump search will find the value of 55 with the following steps assuming that the block size to be jumped is 4.
STEP 1: Jump from index 0 to index 4;
STEP 2: Jump from index 4 to index 8;
STEP 3: Jump from index 8 to index 16;
STEP 4: Since the element at index 16 is greater than 55 we will jump back a step to come to index 9.
STEP 5: Perform linear search from index 9 to get the element 55.
What is the optimal block size to be skipped?
In the worst case, we have to do n/m jumps and if the last checked value is greater than the element to be searched for, we perform m-1 comparisons more for linear search. Therefore the total number of comparisons in the worst case will be ((n/m) + m-1). The value of the function ((n/m) + m-1) will be minimum when m = √n. Therefore, the best step size is m = √n.

Algorithm

Imagine you are given a sorted array, and you are applying linear search to find a value. Jump Search is an improvement to this scenario. Instead of searching one-by-one, we search k-by-k. Let’s say we have a sorted array A, then jump search will look at A[1], A[1 + k], A[1 + 2k], A[1 + 3k] … and so on.

As we keep jumping, we keep a note of the previous value and its index. Once we get a situation where A[i] < searchValue < A[i + k], then it means the value we want is in between the previous jump ends. So now we could simply perform a linear search between that A[i] and A[i + k].
// 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);
    }

}
Output:
Number 55 is at index 10
/* ===== ===== =====

Jump Search Algorithm

===== ===== ===== */

public class JumpSearch {

    public static void main(String[] args) {
        int[] arr = {1, 3, 5, 7, 9, 11, 13, 15};

        System.out.println(jumpSearch(arr, 0)); // -1
        System.out.println(jumpSearch(arr, 3)); // 1
        System.out.println(jumpSearch(arr, 11)); // 5
        System.out.println(jumpSearch(arr, 16)); // -1
    }

    public static int jumpSearch(int[] arr, int val) {
        int jump = (int) Math.sqrt(arr.length);
        int left = 0;
        int right = 0;

        while (left < arr.length && arr[left] <= val) {
            right = Math.min(arr.length - 1, left + jump);

            if (arr[left] <= val && arr[right] >= val) {
                break;
            }

            left += jump;
        }

        if (left >= arr.length || arr[left] > val) {
            return -1;
        }

        right = Math.min(arr.length - 1, right);

        for (int i = left; i <= right && arr[i] <= val; ++i) {
            if (arr[i] == val) {
                return i;
            }
        }

        return -1;
    }

}

No comments:

Post a Comment

Facebook