Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/main/java/com/thealgorithms/searchs/ExponentialSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.thealgorithms.searchs;

import java.util.Arrays;
Copy link
Collaborator

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?


public class ExponentialSearch {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

@DenizAltunkapan DenizAltunkapan Apr 12, 2025

Choose a reason for hiding this comment

The 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);
}
}
}
Loading