- Notifications
You must be signed in to change notification settings - Fork20.1k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
Fahham2903 wants to merge11 commits intoTheAlgorithms:masterChoose a base branch fromFahham2903:add-jump-search
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
11 commits Select commitHold shift + click to select a range
89a050f
Add Jump Search algorithm in Java
5d9eb00
Fix formatting, structure, and docstring for Jump Search
ba7cb8f
Fix structure and package for Jump Search algorithm
2c5717f
Fix formatting and structure for Jump Search
49a2d00
Fix formatting as per CI linter rules
318d2bc
Remove main method and apply final formatting for Jump Search
4d4ab19
Fix warning: explicit cast from double to int
4b444f4
Fix syntax and add find(Integer[], Integer) method
a7ba4ed
Fix: call static method correctly in test file
2994ade
Fix: prevent instantiation of utility class JumpSearch
96e036e
Fix: mark JumpSearch as final utility class
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,44 @@ | ||
package com.thealgorithms.searches; | ||
/** | ||
* Implementation of Jump Search algorithm. | ||
* | ||
* Time Complexity: O(√n) | ||
* Space Complexity: O(1) | ||
* | ||
* Reference: https://en.wikipedia.org/wiki/Jump_search | ||
*/ | ||
public final class JumpSearch { | ||
// Prevent instantiation | ||
private JumpSearch() { | ||
throw new UnsupportedOperationException("Utility class"); | ||
} | ||
public static int jumpSearch(int[] arr, int target) { | ||
int n = arr.length; | ||
int step = (int) Math.floor(Math.sqrt(n)); | ||
int prev = 0; | ||
while (prev < n && arr[Math.min(step, n) - 1] < target) { | ||
prev = step; | ||
step += (int) Math.floor(Math.sqrt(n)); | ||
} | ||
for (int i = prev; i < Math.min(step, n); i++) { | ||
if (arr[i] == target) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
public static int find(Integer[] arr, Integer target) { | ||
int[] array = new int[arr.length]; | ||
for (int i = 0; i < arr.length; i++) { | ||
array[i] = arr[i]; | ||
} | ||
return jumpSearch(array, target); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.