From 3fc70f1ccd93da57b2d4c101a13db1d8b1001002 Mon Sep 17 00:00:00 2001 From: Arpita23r Date: Thu, 9 Oct 2025 20:12:03 +0530 Subject: [PATCH 1/2] added ternary search --- searches/ternary.search.R | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 searches/ternary.search.R diff --git a/searches/ternary.search.R b/searches/ternary.search.R new file mode 100644 index 00000000..827cfdf5 --- /dev/null +++ b/searches/ternary.search.R @@ -0,0 +1,27 @@ +# Ternary Search in R – works on sorted arrays + +ternary_search <- function(arr, target) { + l <- 1 + r <- length(arr) + while (r >= l) { + mid1 <- l + (r - l) %/% 3 + mid2 <- r - (r - l) %/% 3 + if (arr[mid1] == target) return(mid1) + if (arr[mid2] == target) return(mid2) + if (target < arr[mid1]) r <- mid1 - 1 + else if (target > arr[mid2]) l <- mid2 + 1 + else { + l <- mid1 + 1 + r <- mid2 - 1 + } + } + return(-1) +} + +arr <- as.integer(strsplit(readline("Enter sorted integers: "), " ")[[1]]) +target <- as.integer(readline("Enter target to search: ")) +index <- ternary_search(arr, target) +if (index != -1) + cat("Element found at position:", index, "\n") +else + cat("Element not found.\n") \ No newline at end of file From 55e254ddff50312fd11df114f4053009aa290a03 Mon Sep 17 00:00:00 2001 From: Arpita Roy <100989922+Arpita23r@users.noreply.github.com> Date: Sat, 11 Oct 2025 14:42:59 +0530 Subject: [PATCH 2/2] Update ternary.search.R --- searches/ternary.search.R | 40 ++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/searches/ternary.search.R b/searches/ternary.search.R index 827cfdf5..0c2e16a0 100644 --- a/searches/ternary.search.R +++ b/searches/ternary.search.R @@ -1,16 +1,26 @@ +#' Performs ternary search on a sorted integer array. +#' +#' @param arr Integer vector. Must be sorted in ascending order. +#' @param target Integer value to search for in arr. +#' @return The index (1-based) of the target in arr if found, otherwise -1. +#' @details The input array must be sorted in ascending order for correct results. # Ternary Search in R – works on sorted arrays ternary_search <- function(arr, target) { l <- 1 r <- length(arr) - while (r >= l) { + while (l <= r) { mid1 <- l + (r - l) %/% 3 mid2 <- r - (r - l) %/% 3 + if (arr[mid1] == target) return(mid1) if (arr[mid2] == target) return(mid2) - if (target < arr[mid1]) r <- mid1 - 1 - else if (target > arr[mid2]) l <- mid2 + 1 - else { + + if (target < arr[mid1]) { + r <- mid1 - 1 + } else if (target > arr[mid2]) { + l <- mid2 + 1 + } else { l <- mid1 + 1 r <- mid2 - 1 } @@ -18,10 +28,26 @@ ternary_search <- function(arr, target) { return(-1) } -arr <- as.integer(strsplit(readline("Enter sorted integers: "), " ")[[1]]) -target <- as.integer(readline("Enter target to search: ")) +# --- User Input Section with Validation --- +arr_input <- readline("Enter sorted integers: ") +arr_split <- strsplit(arr_input, " ")[[1]] +arr <- as.integer(arr_split) +if (length(arr) == 0 || any(is.na(arr))) { + cat("Error: Please enter a non-empty list of valid integers separated by spaces.\n") + quit(status = 1) +} + +target_input <- readline("Enter target to search: ") +target <- suppressWarnings(as.integer(target_input)) +if (is.na(target_input) || is.na(target)) { + cat("Error: Please enter a valid integer for the target.\n") + quit(status = 1) +} + +# --- Execute Search --- index <- ternary_search(arr, target) + if (index != -1) cat("Element found at position:", index, "\n") else - cat("Element not found.\n") \ No newline at end of file + cat("Element not found.\n")