From 12ef1b792524f53ee1f45947e49b7bc46f954786 Mon Sep 17 00:00:00 2001 From: sri vatsav Date: Mon, 15 Jul 2019 18:52:05 +0530 Subject: [PATCH] Jump search Code in Java (#30962) Code for Jump Search in Java. --- .../search-algorithms/jump-search/index.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/guide/english/algorithms/search-algorithms/jump-search/index.md b/guide/english/algorithms/search-algorithms/jump-search/index.md index b3f8a27822..6bd0c1d06a 100644 --- a/guide/english/algorithms/search-algorithms/jump-search/index.md +++ b/guide/english/algorithms/search-algorithms/jump-search/index.md @@ -17,6 +17,42 @@ O(√N) ![Jumping Search 1](https://i1.wp.com/theoryofprogramming.com/wp-content/uploads/2016/11/jump-search-1.jpg?resize=676%2C290) +# Code In Java. + +``` Java + +public int jumpSearch(int[] arr, int x) +{ + int n = arr.length; + + // Finding the size to be jumped + int jumpSize = (int) Math.floor(Math.sqrt(n)); + + // Finding the index where element is present + int index = 0; + while (arr[Math.min(jumpSize, n)-1] < x) + { + index = jumpSize; + jumpSize += (int) Math.floor(Math.sqrt(n)); + if (index >= n) + return -1; + } + // Searching for x beginning with index. + while (arr[index] < x) + { + index++; + // If we reached next index or end of array then element is not present. + if (index == Math.min(jumpSize, n)) + return -1; + } + // If element is found + if (arr[index] == x) + return index; + return -1; +} + +``` + # Code To view examples of code implementation for this method, access this link below: