-
-
Notifications
You must be signed in to change notification settings - Fork 331
Add Ternary Search algorithm for optimization and array searching [HACKTOBERFEST 2025] #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Ternary Search algorithm for optimization and array searching [HACKTOBERFEST 2025] #156
Conversation
- 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
There was a problem hiding this 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.
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 | ||
} | ||
} |
Copilot
AI
Oct 4, 2025
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment addressed?
There was a problem hiding this comment.
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.
Please check comments |
@siriak all conflicts resolved |
π 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
π― Why This Matters
Ternary Search is essential for:
π Implementation Details
π§ Core Functions
ternary_search_maximum()
- Find maximum of unimodal functionsternary_search_minimum()
- Find minimum of unimodal functionsternary_search_array_recursive()
- Recursive array searchingternary_search_array_iterative()
- Iterative array searchingternary_search_with_count()
- Performance analysis versionπ§ͺ Comprehensive Examples
π Algorithm Comparison
Ternary vs Binary Search:
π― Optimization Applications
Production Cost Example: