R Program to Implement Exponential Search

R Program to Implement Exponential Search

Exponential Search is an efficient algorithm used to find a specific element in a sorted array. Unlike Binary Search, which divides the array in half each time, Exponential Search first identifies a range where the target might exist by increasing the index exponentially. Once the range is located, it performs a Binary Search within that range. This approach is especially useful for large datasets where elements are spread across a wide range, as it quickly narrows down the area to search.

For beginners learning R programming, Exponential Search is a great way to understand how algorithms can combine strategies—here, exponential indexing and binary search—to optimize performance. It is commonly applied in scenarios like searching sorted records in databases, numeric datasets, or time-series data, where direct scanning of all elements would be inefficient. Practicing this algorithm helps learners build a strong foundation in recursion, iteration, and array handling in R.

Program 1: Basic Exponential Search using Binary Search

This program demonstrates Exponential Search combined with Binary Search to locate an element in a sorted array.

binary_search <- function(arr, target, left, right) {

  while (left <= right) {

    mid <- floor((left + right) / 2)

    if (arr[mid] == target) return(mid)
    else if (arr[mid] < target) left <- mid + 1
    else right <- mid - 1

  }

  -1

}

exponential_search <- function(arr, target) {

  if (length(arr) == 0) return(-1)
  if (target < arr[1]) return(-1)
  if (arr[1] == target) return(1)

  index <- 1

  while (index <= length(arr) && arr[index] < target) {
    index <- index * 2
  }

  left <- floor(index / 2)
  right <- min(index, length(arr))

  binary_search(arr, target, left, right)

}

data <- c(1, 3, 5, 7, 9, 11, 13)
target <- 7

print(exponential_search(data, target))

This program works by first finding an approximate range exponentially, then refining the search using Binary Search. Beginners can understand that Exponential Search is a two-step process that is particularly fast for elements near the start of large arrays.

Program 2: Exponential Search with recursion

Instead of using a loop for Binary Search, this version applies recursion to perform the search within the range.

binary_search_rec <- function(arr, target, left, right) {

  if (left > right) return(-1)

  mid <- floor((left + right) / 2)

  if (arr[mid] == target) return(mid)
  else if (arr[mid] < target)
    return(binary_search_rec(arr, target, mid + 1, right))
  else
    return(binary_search_rec(arr, target, left, mid - 1))

}

exponential_search_rec <- function(arr, target) {

  if (arr[1] == target) return(1)

  index <- 1

  while (index <= length(arr) && arr[index] < target) {
    index <- index * 2
  }

  left <- floor(index / 2)
  right <- min(index, length(arr))

  binary_search_rec(arr, target, left, right)

}

data <- c(2, 4, 6, 8, 10, 12)
target <- 4

print(exponential_search_rec(data, target))

Recursion allows beginners to see how the same logic can be implemented without loops. This approach is elegant and aligns with common algorithmic practices in computer science.

Program 3: Exponential Search with negative numbers

Exponential Search works with negative numbers as long as the array is sorted.

binary_search <- function(arr, target, left, right) {

  while (left <= right) {

    mid <- floor((left + right) / 2)

    if (arr[mid] == target) return(mid)
    else if (arr[mid] < target) left <- mid + 1
    else right <- mid - 1

  }

  -1

}

exponential_search <- function(arr, target) {

  if (length(arr) == 0) return(-1)
  if (target < arr[1]) return(-1)
  if (arr[1] == target) return(1)

  index <- 1

  while (index <= length(arr) && arr[index] < target) {
    index <- index * 2
  }

  left <- floor(index / 2)
  right <- min(index, length(arr))

  binary_search(arr, target, left, right)

}

data <- c(-50, -40, -30, -20, -10, 0)
target <- -30

print(exponential_search(data, target))

This program demonstrates that the algorithm is versatile and can be applied to datasets containing negative values, such as financial data or temperature measurements.

Program 4: Exponential Search with floating-point numbers

The algorithm can also handle decimal numbers, which is useful for scientific or financial datasets.

binary_search <- function(arr, target, left, right) {

  while (left <= right) {

    mid <- floor((left + right) / 2)

    if (arr[mid] == target) return(mid)
    else if (arr[mid] < target) left <- mid + 1
    else right <- mid - 1

  }

  -1

}

exponential_search <- function(arr, target) {

  if (length(arr) == 0) return(-1)
  if (target < arr[1]) return(-1)
  if (arr[1] == target) return(1)

  index <- 1

  while (index <= length(arr) && arr[index] < target) {
    index <- index * 2
  }

  left <- floor(index / 2)
  right <- min(index, length(arr))

  binary_search(arr, target, left, right)

}

data <- c(0.5, 1.2, 2.4, 3.6, 4.8)
target <- 3.6

print(exponential_search(data, target))

Beginners can see that the logic remains the same for floating-point numbers, reinforcing the idea that search algorithms are flexible across numeric types.

Program 5: Exponential Search for first occurrence of duplicates

This version ensures that the first occurrence of a duplicate element is returned.

binary_search <- function(arr, target, left, right) {

  while (left <= right) {

    mid <- floor((left + right) / 2)

    if (arr[mid] == target) return(mid)
    else if (arr[mid] < target) left <- mid + 1
    else right <- mid - 1

  }

  -1

}

exponential_search <- function(arr, target) {

  if (length(arr) == 0) return(-1)
  if (target < arr[1]) return(-1)
  if (arr[1] == target) return(1)

  index <- 1

  while (index <= length(arr) && arr[index] < target) {
    index <- index * 2
  }

  left <- floor(index / 2)
  right <- min(index, length(arr))

  binary_search(arr, target, left, right)

}

exponential_search_first <- function(arr, target) {

  pos <- exponential_search(arr, target)

  if (pos == -1) return(-1)

  while (pos > 1 && arr[pos - 1] == target) {
    pos <- pos - 1
  }

  return(pos)

}

data <- c(1, 2, 2, 2, 3, 4)
target <- 2

print(exponential_search_first(data, target))

This shows beginners how small modifications can adapt the algorithm for real-world situations where duplicates exist.

Program 6: Input validation for Exponential Search

Adding validation ensures the array is sorted in ascending order, which prevents unexpected results.

binary_search <- function(arr, target, left, right) {

  while (left <= right) {

    mid <- floor((left + right) / 2)

    if (arr[mid] == target) return(mid)
    else if (arr[mid] < target) left <- mid + 1
    else right <- mid - 1

  }

  -1

}

exponential_search <- function(arr, target) {

  if (length(arr) == 0) return(-1)
  if (target < arr[1]) return(-1)
  if (arr[1] == target) return(1)

  index <- 1

  while (index <= length(arr) && arr[index] < target) {
    index <- index * 2
  }

  left <- floor(index / 2)
  right <- min(index, length(arr))

  binary_search(arr, target, left, right)

}

exponential_search_safe <- function(arr, target) {

  if (!all(diff(arr) >= 0)) stop("Array must be sorted in ascending order")

  exponential_search(arr, target)

}

data <- c(1, 3, 5, 7)
target <- 5

print(exponential_search_safe(data, target))

This program teaches beginners the importance of input checks when implementing algorithms.

Program 7: Exponential Search on descending arrays

With minor changes, Exponential Search can handle arrays sorted in descending order.

binary_search_desc <- function(arr, target, left, right) {

  while (left <= right) {

    mid <- floor((left + right) / 2)

    if (arr[mid] == target) return(mid)
    else if (arr[mid] > target) left <- mid + 1
    else right <- mid - 1

  }

  -1
}

exponential_search_desc <- function(arr, target) {

  if (length(arr) == 0) return(-1)
  if (arr[1] == target) return(1)
  if (target > arr[1]) return(-1)

  index <- 1

  while (index <= length(arr) && arr[index] > target) {
    index <- index * 2
  }

  left <- floor(index / 2)
  right <- min(index, length(arr))

  binary_search_desc(arr, target, left, right)

}

data <- c(100, 80, 60, 40, 20)
target <- 100

print(exponential_search_desc(data, target))

This teaches beginners that algorithms can be adapted to different sorting orders with minimal logic changes.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about Exponential Search in R.

Q1. Can Exponential Search work on unsorted arrays?
No, it requires a sorted array to locate the target correctly.

Q2. How does Exponential Search differ from Binary Search?
It first finds a range exponentially before applying Binary Search, which can reduce comparisons for elements near the start.

Q3. Can it handle negative or decimal numbers?
Yes, as long as the array is sorted, Exponential Search works with any numeric values.

Q4. Can Exponential Search find the first occurrence among duplicates?
Yes, with minor modifications, it can locate the first occurrence in arrays with duplicate values.

Conclusion

Exponential Search is a practical algorithm for efficiently locating elements in sorted arrays by combining exponential range identification with Binary Search. Through these examples, beginners can explore both iterative and recursive implementations, work with negative and floating-point numbers, handle duplicates, and adapt the algorithm for descending arrays.

Practicing Exponential Search in R strengthens understanding of search algorithms, recursion, iteration, and array handling. It’s a valuable skill for anyone looking to work with large datasets efficiently and builds a foundation for learning more advanced algorithmic techniques.

Scroll to Top