Skip to content
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

Implement Exponential Search Algorithm (fixes #54) #124

Conversation

saurabh12nxf
Copy link
Contributor

This pull request addresses issue #54 by implementing the Exponential Search Algorithm. I have a solid understanding of searching algorithms and am confident in delivering an efficient solution.

High-Level Approach
Exponential Search Function:

Created a function exponentialSearch to locate the range in which the target element might be present.

Performed a binary search within that interval to find the target element.

Binary Search Function:

Created a helper function binarySearch to conduct the search within the identified range from the Exponential Search Function.

Testing the Implementation:

Built test sorted arrays and thoroughly tested the functions to ensure proper working.

Detailed Implementation
Exponential Search Function

java
public static int exponentialSearch(int[] arr, int target) {
int n = arr.length;
if (arr[0] == target) {
return 0;
}
int i = 1;
while (i < n && arr[i] <= target) {
i = i * 2;
}
return binarySearch(arr, target, i / 2, Math.min(i, n - 1));
}
Binary Search Function

java
public static int binarySearch(int[] arr, int target, int left, int right) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // target not found
}
Main Method for Testing

java
public static void main(String[] args) {
int[] arr = {2, 3, 4, 10, 40, 50, 60, 70, 80, 90};
int target = 10;
int result = exponentialSearch(arr, target);
if (result != -1) {
System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found in array.");
}
}
Testing
Created test arrays with different target values to verify the implementation.

Ensured that both edge cases and typical scenarios are handled correctly.

Verified that the solution works efficiently on large arrays.

@abhishektripathi66 abhishektripathi66 merged commit 955d3ae into abhishektripathi66:master Feb 21, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants