Add Ternary Search algorithm for optimization and array searching [HACKTOBERFEST 2025]#156
Add Ternary Search algorithm for optimization and array searching [HACKTOBERFEST 2025]#156piyushkumar0707 wants to merge 4 commits intoTheAlgorithms:masterfrom
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.
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 | ||
| } | ||
| } |
There was a problem hiding this comment.
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 | |
| } | |
| } |
There was a problem hiding this comment.
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: