-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Implemented Ternary Search Algorithm #24
Implemented Ternary Search Algorithm #24
Conversation
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.
Hi @SatinWukerORIG ,
Looking forward to your review 🙂
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.
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 code looks good! But why are you adding a "_based" at the end of the file name?
Thanks! 👍 I just added it to easily differentiate between the two use cases of the algorithm. The algorithm can be used to sort arrays as well as searching for min/max points in unimodal functions. Putting them in one program is possible, but will just make the demo too long. We can enumerate them instead "_1" and "_2". |
Description:
This pull request introduces an implementation of the Ternary Search algorithm.
It considers both array-based approach for searching elements within a sorted array and function-based approach for finding extremum points.
Definition:
Ternary Search is a divide-and-conquer algorithm used to search for an element in a sorted array or to find the minimum/maximum of unimodal functions.
It divides the search range into three parts and recursively eliminates two-thirds of the search space.
Array-based Ternary Search works similarly to binary search, but it splits the array into three parts instead of two, allowing for potentially fewer comparisons in large datasets.
Function-based Ternary Search finds the minimum or maximum value of a unimodal function over a given interval, narrowing the search range by evaluating two intermediate points.
Time Complexity:
O(log3(n))
, wheren
is the number of elements.tol
) specified.Implementation Details:
ternary_search.f90
: Contains theternary_search_module
with:The module supports searching in arrays and unimodal functions with customisable precision for the function-based search.
Notes
The array-based ternary search requires the input array to be sorted. If the array is unsorted, the search results will be incorrect. A sorting algorithm (e.g., Implemented Heap Sort Algorithm #8 or Implemented Merge Sort Algorithm #7) must be applied to the array before using ternary search.
The function-based ternary search assumes that the input function is unimodal (i.e., it has only one minimum or maximum in the interval). If the function is not unimodal, the algorithm is not relevant.
Example Usage:
example_ternary_search_array.f90
: Demonstrates the usage of ternary_search_array:ternary_search_function_based.f90
: Demonstrates the usage ofternary_search_minimum
andternary_search_maximum
:Reference
The Algorithm Design Manual, Latest edition