Exponential Search is an algorithm that is particularly useful for searching in sorted arrays. It combines the power of binary search with exponential growth to quickly find the range where the element might be present, then performs a binary search within that range.
The algorithm begins with a bound of 1 and doubles it until the bound exceeds the length of the array or the element at that position is greater than the target value. Once the suitable range is found, a binary search is performed in that range.
public class ExponentialSearch {
public static int exponentialSearch(int arr[], int n, int x) {
if (arr[0] == x) return 0;
int i = 1;
while (i < n && arr[i] <= x) i = i * 2;
return binarySearch(arr, i / 2, Math.min(i, n), x);
}
public static int binarySearch(int arr[], int low, int high, int x) {
if (high >= low) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, low, mid - 1, x);
return binarySearch(arr, mid + 1, high, x);
}
return -1;
}
public static void main(String args[]) {
int arr[] = {2, 3, 4, 10, 40};
int x = 10;
int result = exponentialSearch(arr, arr.length, x);
System.out.println((result < 0) ? "Element not found" : "Element found at index " + result);
}
}
In this example, we search for the number 10 in a sorted array {2, 3, 4, 10, 40}. The exponential search quickly identifies the range and the binary search confirms its position at index 3.
If we search for an element not present in the array, such as 5, the exponential search will determine the range and the binary search will return -1, indicating the element is not found.
Console Output:
Element found at index 3
Exponential Search is especially effective in large datasets where the element is closer to the beginning, as it can quickly identify the range and perform a binary search within it.
public class LargeArraySearch {
public static void main(String args[]) {
int arr[] = new int[1000];
for (int i = 0; i < 1000; i++) arr[i] = i * 2;
int x = 198;
int result = ExponentialSearch.exponentialSearch(arr, arr.length, x);
System.out.println((result < 0) ? "Element not found" : "Element found at index " + result);
}
}
This example demonstrates searching for the number 198 in an array of 1000 elements. The exponential search efficiently narrows down the range, allowing the binary search to quickly find the element at index 99.
Console Output:
Element found at index 99
When dealing with duplicate elements, exponential search will find the first occurrence in the range identified. If multiple occurrences exist, further logic might be needed to identify all instances.
public class DuplicateElementSearch {
public static void main(String args[]) {
int arr[] = {1, 2, 4, 4, 4, 5, 6, 7};
int x = 4;
int result = ExponentialSearch.exponentialSearch(arr, arr.length, x);
System.out.println((result < 0) ? "Element not found" : "First occurrence at index " + result);
}
}
In this example, we search for the number 4 in an array containing duplicates. The exponential search identifies the range and the binary search finds the first occurrence at index 2.
Console Output:
First occurrence at index 2
Sparse arrays, which contain many empty or null elements, can benefit from exponential search by quickly skipping over large sections of empty data to find the relevant range.
public class SparseArraySearch {
public static void main(String args[]) {
String arr[] = {"", "", "", "apple", "", "", "banana", "", "cherry"};
String x = "banana";
int result = ExponentialSearch.exponentialSearch(arr, arr.length, x);
System.out.println((result < 0) ? "Element not found" : "Element found at index " + result);
}
}
This example searches for the word "banana" in a sparse array. Exponential search efficiently identifies the range, allowing the binary search to locate the element at index 6.
Console Output:
Element found at index 6
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies