filmov
tv
Ternary Search in 4 min (Python)
Показать описание
In this video, I will explain ternary search using a while loop and then using the recursive way. Specifically, I will be demonstrating the ternary search algorithm in a program iteratively (using a loop) and then recursively (using recursion).
The way ternary search works is that you have a number that you want to search for in an array or a list. This array must be sorted from least to greatest as a precondition. If the number exists in the sorted array, you return the location (which is the index) where that number occurs. We call this number "the target". If the target does not exist inside the array, you simply return -1. Ternary search is amazing, with logarithmic run time.
What is recursion? Recursion is a function that calls upon itself. Here, we write a function that calls upon itself to ternary search an element or value inside an array using a for loop and using recursion. While it is easy to use a for loop or while loop to do this, it is important that you understand these recursion steps to have a strong foundation to learn more complex recursive algorithm in the future.
In the next video, I will explain binary search and linear search which is very similar to linear search, and I will also explain why it is so useful.
LIKE & SUBSCRIBE:
The way ternary search works is that you have a number that you want to search for in an array or a list. This array must be sorted from least to greatest as a precondition. If the number exists in the sorted array, you return the location (which is the index) where that number occurs. We call this number "the target". If the target does not exist inside the array, you simply return -1. Ternary search is amazing, with logarithmic run time.
What is recursion? Recursion is a function that calls upon itself. Here, we write a function that calls upon itself to ternary search an element or value inside an array using a for loop and using recursion. While it is easy to use a for loop or while loop to do this, it is important that you understand these recursion steps to have a strong foundation to learn more complex recursive algorithm in the future.
In the next video, I will explain binary search and linear search which is very similar to linear search, and I will also explain why it is so useful.
LIKE & SUBSCRIBE:
Комментарии