Exponential Search & Unbounded Binary Search

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 61 :- Exponential Search & Unbounded Binary Search

Exponential Search and Unbounded Binary Search are both searching algorithms used to find a specific target element in a sorted array, but they have different approaches and use cases. Let's explore each algorithm:

  1. Exponential Search:
    • Exponential Search is an improved version of Binary Search for unbounded or infinite-sized arrays.
    • It works by finding an appropriate range (exponential interval) in which the target element may exist.
    • Initially, the range starts with the first element and keeps doubling until the range's upper bound becomes larger than the target.
    • Once the range is found, Binary Search is performed within that range to locate the target element.
    • This algorithm is particularly useful when the size of the array is not known, or it's prohibitively large to perform Binary Search directly.

Here's the C++ implementation of Exponential Search:

cppCopy code

#include <iostream> #include <vector> int binarySearch(std::vector<int>& arr, int left, int right, int target) {    while (left <= right) {        int mid = left + (right - left) / 2;        if (arr[mid] == target) {            return mid;        } else if (arr[mid] < target) {            left = mid + 1;        } else {            right = mid - 1;        }    }    return -1; } int exponentialSearch(std::vector<int>& arr, int target) {    int n = arr.size();    if (arr[0] == target) {        return 0;    }    int i = 1;    while (i < n && arr[i] <= target) {        i *= 2;    }    return binarySearch(arr, i / 2, std::min(i, n - 1), target); } int main() {    std::vector<int> arr = {2, 4, 6, 8, 10, 12, 14, 16};    int target = 8;    int index = exponentialSearch(arr, target);    if (index != -1) {        std::cout << "Element found at index " << index << std::endl;    } else {        std::cout << "Element not found in the array." << std::endl;    }    return 0; }

  1. Unbounded Binary Search:
    • Unbounded Binary Search is used to find the position of an element in an infinite-sized sorted array or an array with unknown size.
    • The algorithm starts by finding a range in which the target may exist. It keeps doubling the range until it finds an upper bound larger than the target.
    • After finding the range, it performs Binary Search within that range to locate the target element.
    • This algorithm is useful when dealing with data structures like linked lists, which can be traversed but do not allow direct access to elements.

Both Exponential Search and Unbounded Binary Search are useful in specific scenarios when dealing with large or unknown-sized datasets. They provide efficient ways to locate elements without having to traverse the entire dataset linearly.

9. Week4 - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?