Skip to content

Add Jump Search algorithm in Java #6407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Next Next commit
Add Jump Search algorithm in Java
  • Loading branch information
Fahham29 committed Jul 18, 2025
commit 89a050ff9cd256a81e795f8b79bbc7df7c18480d
31 changes: 31 additions & 0 deletions src/search/JumpSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package search;

public class JumpSearch {

public static int jumpSearch(int[] arr, int target) {
int n = arr.length;
int step = (int) Math.floor(Math.sqrt(n));
int prev = 0;

while (arr[Math.min(step, n)-1] < target) {
prev = step;
step += Math.floor(Math.sqrt(n));
if (prev >= n)
return -1;
}

for (int i = prev; i < Math.min(step, n); i++) {
if (arr[i] == target)
return i;
}

return -1;
}

public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9, 12, 17, 21, 25};
int target = 12;
int index = jumpSearch(arr, target);
System.out.println("Found at index: " + index);
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy