-
Notifications
You must be signed in to change notification settings - Fork 19.9k
Add exponential search algorithm #6218
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.thealgorithms.searchs; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ExponentialSearch { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to insert a private constructor because the default constructor from the Object class is public |
||
// Exponential Search algorithm | ||
public static int exponentialSearch(int[] arr, int x) { | ||
int n = arr.length; | ||
|
||
// If the element is present at the first position | ||
if (arr[0] == x) return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's recommended to use curly braces for 'if' blocks, even for single statements |
||
|
||
// Find the range for binary search by repeated doubling | ||
int i = 1; | ||
while (i < n && arr[i] <= x) { | ||
i = i * 2; | ||
} | ||
|
||
// Do binary search in the found range | ||
return binarySearch(arr, x, i / 2, Math.min(i, n - 1)); | ||
} | ||
|
||
// Binary Search algorithm | ||
private static int binarySearch(int[] arr, int x, int low, int high) { | ||
while (low <= high) { | ||
int mid = low + (high - low) / 2; | ||
|
||
if (arr[mid] == x) return mid; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are also here curly braces needed |
||
|
||
if (arr[mid] < x) low = mid + 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's necessary to use curly braces for 'if' and 'else' blocks, even for single statements |
||
else high = mid - 1; | ||
} | ||
return -1; // Element not found | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] arr = {1, 3, 5, 7, 9, 11, 15, 17, 19, 21}; | ||
int x = 15; | ||
|
||
int result = exponentialSearch(arr, x); | ||
|
||
if (result == -1) { | ||
System.out.println("Element not found."); | ||
} else { | ||
System.out.println("Element found at index " + result); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there might be an unused import. Is this necessary?