Ternary Search in 4 min (Python)

preview_player
Показать описание
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:
Рекомендации по теме
Комментарии
Автор

This is VERY well explained! You deserve a gold medal

HappyDishHappyLife
Автор

One of the best explanations ever saw in the YouTube you explained ternary search+ explained iterative and recursive code in just less than 5 mins is brilliant.

nikolatesla
Автор

4 minutes of pure no bullshit content, THANKS in all caps

MICHAELMURITHI-yy
Автор

just subscribed now and finally i found best explained tutorial

VirtualMate
Автор

The explanation is excelent. Thank you

mariamakter
Автор

Very good explanation.
What's the advantage of using Ternary Search over Binary Search, though? They both require the array to be sorted, and they both seem to have more or less the same time complexity. In terms of difficulty to understand, though, Ternary Search is far less intuitive and easy-to-understand than Binary Search, so under what circumstances would you choose Ternary over Binary?

Ashadow
Автор

Can u make a video about Jump search, please?

ohhellnah
Автор

Please Teach us Meta Binary search (One-Sided Binary search)

MICHAELMURITHI-yy
Автор

func TernarySearch(arr []int, length int, target int, mid1 int, mid2 int) int {
if length != 0 {
if target >= arr[0] && target <= arr[len(arr)-1] {
if target == arr[mid1] || target == arr[mid2] {
if target == arr[mid1] {
mid1
} else {
mid2
}
}
if target < arr[mid1] {
return TernarySearch(arr[:mid1], length, target, mid1/3, mid1/2)
} else if target > arr[mid2] {
return TernarySearch(arr[mid2:], length, target, len(arr)/3, len(arr)/2) + length/2
} else if arr[mid1] < target && target < arr[mid2] {
return TernarySearch(arr[mid1:mid2-1], length, target, len(arr)/3, len(arr)/2) + length/3
}
}
}
return -1
}
func main() {
var arr = []int{1, 2, 3, 4, 5}
fmt.Println(TernarySearch(arr, len(arr), 6, len(arr)/3, len(arr)/2))
}

And here is my version of implementation in Go.

shouvikxiv