Skip to content
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

Conversation

Ramy-Badr-Ahmed
Copy link
Member

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed commented Sep 20, 2024

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:

    • Array-based: O(log3(n)), where n is the number of elements.
    • Function-based: Dependent on the precision (tol) specified.

Implementation Details:

ternary_search.f90: Contains the ternary_search_module with:

ternary_search_array: A recursive function that searches for a target element in a sorted array by dividing it into three parts.
ternary_search_minimum: A recursive function that finds the minimum of a unimodal function by evaluating it in smaller intervals.
ternary_search_maximum: A recursive function that finds the maximum of a unimodal function using a similar approach as the minimum search.

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:

  • Searches for a target value in a sorted array:
Array: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Target: 17

Output: Target found at index: 9

ternary_search_function_based.f90: Demonstrates the usage of ternary_search_minimum and ternary_search_maximum:

  • Finding Minimum:
Function: (x - 5.0)**2 + cos(x)
Range: [0.0, 10.0]

Output: Minimum at x ≈ 4.51 with value ≈ 0.0391.
  • Finding Maximum:
Function: -(x - 5.0)**2 + cos(x)
Range: [0.0, 10.0]

Output: Maximum at x ≈ 5.39 with value ≈ 0.4748

Reference

The Algorithm Design Manual, Latest edition

Copy link
Member Author

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed left a 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 🙂

Copy link
Member Author

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed left a comment

Choose a reason for hiding this comment

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

@SatinWukerORIG

New #13 checks are passing:

ci

Considered fprettify #27

All checks have passed 👍

Copy link
Member

@SatinWukerORIG SatinWukerORIG left a 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?

@Ramy-Badr-Ahmed
Copy link
Member Author

Ramy-Badr-Ahmed commented Sep 27, 2024

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".

@SatinWukerORIG SatinWukerORIG merged commit ec8fe1c into TheAlgorithms:main Sep 29, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants