Skip to content

Conversation

piyushkumar0707
Copy link
Contributor

πŸš€ Overview

This PR implements the Ternary Search algorithm - a divide-and-conquer technique that extends beyond simple searching to solve optimization problems on unimodal functions.

✨ Features

  • βœ… Unimodal function optimization - Find maximum/minimum of single-peaked functions
  • βœ… Array searching - Alternative to binary search with 3-way division
  • βœ… Recursive & iterative implementations - Both approaches for educational comparison
  • βœ… Real-world optimization examples - Production cost minimization
  • βœ… Performance comparison - Benchmarks against binary search
  • βœ… Comprehensive testing - Edge cases and various function types

🎯 Why This Matters

Ternary Search is essential for:

  • Mathematical optimization - Finding optimal solutions in unimodal functions
  • Competitive programming - Common technique in programming contests
  • Engineering applications - Cost minimization, resource optimization
  • Machine learning - Hyperparameter tuning for unimodal loss functions
  • Scientific computing - Finding extrema in experimental data
  • Algorithm education - Understanding divide-and-conquer variants

πŸ“š Implementation Details

  • Time Complexity: O(log₃ n) β‰ˆ O(log n) for both searching and optimization
  • Space Complexity: O(1) iterative, O(log n) recursive
  • Algorithm: Divides search space into three equal parts instead of two
  • Precision Control: Configurable precision for optimization problems

πŸ”§ Core Functions

  • ternary_search_maximum() - Find maximum of unimodal functions
  • ternary_search_minimum() - Find minimum of unimodal functions
  • ternary_search_array_recursive() - Recursive array searching
  • ternary_search_array_iterative() - Iterative array searching
  • ternary_search_with_count() - Performance analysis version

πŸ§ͺ Comprehensive Examples

  • Quadratic function optimization - Finding vertex of parabolas
  • Array searching - Comparison with binary search performance
  • Real optimization problem - Production cost minimization
  • Edge cases - Empty arrays, single elements, not found scenarios
  • Performance benchmarking - Comparison counts with binary search

πŸ“Š Algorithm Comparison

Ternary vs Binary Search:

  • Ternary: ~2 comparisons per iteration, reduces to 1/3 each time
  • Binary: ~1 comparison per iteration, reduces to 1/2 each time
  • Trade-off: More comparisons per iteration but faster convergence

🎯 Optimization Applications

Production Cost Example:

cost_function <- function(x) 100 + 2*x + 0.001*x^2 - 0.1*x*log(x + 1)
optimal_quantity <- ternary_search_minimum(cost_function, 1, 1000)

- Implement ternary search for unimodal function optimization
- Include both recursive and iterative array search versions
- Add maximum/minimum finding for optimization problems
- Performance comparison with binary search
- Comprehensive examples including real-world optimization
- Update DIRECTORY.md with new search algorithm
@Copilot Copilot AI review requested due to automatic review settings October 4, 2025 18:57
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a comprehensive Ternary Search algorithm implementation in R, adding both optimization and array searching capabilities to the algorithms collection. The implementation includes multiple variants of ternary search for different use cases.

  • Adds ternary search functions for finding maxima/minima of unimodal functions
  • Implements both recursive and iterative array searching variants
  • Includes performance comparison utilities and comprehensive examples

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
searches/ternary_search.r Complete ternary search implementation with optimization functions, array searching, performance comparisons, and extensive examples
DIRECTORY.md Adds entry for the new ternary search algorithm in the searches section

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +180 to +200
comparisons <- comparisons + 2 # Two comparisons per iteration

mid1 <- left + floor((right - left) / 3)
mid2 <- right - floor((right - left) / 3)

if (arr[mid1] == target) {
return(list(index = mid1, comparisons = comparisons))
}
if (arr[mid2] == target) {
return(list(index = mid2, comparisons = comparisons))
}

if (target < arr[mid1]) {
right <- mid1 - 1
} else if (target > arr[mid2]) {
left <- mid2 + 1
} else {
left <- mid1 + 1
right <- mid2 - 1
}
}
Copy link

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comparison count is incremented by 2 at the start of each iteration, but the actual number of comparisons varies depending on which conditions are met. The function may perform fewer than 2 comparisons if the target is found at mid1, making the count inaccurate.

Suggested change
comparisons <- comparisons + 2 # Two comparisons per iteration
mid1 <- left + floor((right - left) / 3)
mid2 <- right - floor((right - left) / 3)
if (arr[mid1] == target) {
return(list(index = mid1, comparisons = comparisons))
}
if (arr[mid2] == target) {
return(list(index = mid2, comparisons = comparisons))
}
if (target < arr[mid1]) {
right <- mid1 - 1
} else if (target > arr[mid2]) {
left <- mid2 + 1
} else {
left <- mid1 + 1
right <- mid2 - 1
}
}
mid1 <- left + floor((right - left) / 3)
mid2 <- right - floor((right - left) / 3)
comparisons <- comparisons + 1
if (arr[mid1] == target) {
return(list(index = mid1, comparisons = comparisons))
}
comparisons <- comparisons + 1
if (arr[mid2] == target) {
return(list(index = mid2, comparisons = comparisons))
}
comparisons <- comparisons + 1
if (target < arr[mid1]) {
right <- mid1 - 1
} else {
comparisons <- comparisons + 1
if (target > arr[mid2]) {
left <- mid2 + 1
} else {
left <- mid1 + 1
right <- mid2 - 1
}
}

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment addressed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are just resolving the conversation without addressing the feedback and this has already happened twice. This violates our code of conduct, specifically:

Other conduct which could reasonably be considered inappropriate in a professional setting

Therefore I am closing the pull request and labeling it accordingly.

@siriak
Copy link
Member

siriak commented Oct 4, 2025

Please check comments

@piyushkumar0707
Copy link
Contributor Author

@siriak all conflicts resolved

@siriak siriak added the invalid This doesn't seem right label Oct 7, 2025
@siriak siriak closed this Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

invalid This doesn't seem right

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants