forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request keon#753 from DivyanshMandhan/master
add ternary search
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" | ||
Ternary search is a divide and conquer algorithm that can be used to find an element in an array. | ||
It is similar to binary search where we divide the array into two parts but in this algorithm, | ||
we divide the given array into three parts and determine which has the key (searched element). | ||
We can divide the array into three parts by taking mid1 and mid2. | ||
Initially, l and r will be equal to 0 and n-1 respectively, where n is the length of the array. | ||
mid1 = l + (r-l)/3 | ||
mid2 = r – (r-l)/3 | ||
Note: Array needs to be sorted to perform ternary search on it. | ||
T(N) = O(log3(N)) | ||
log3 = log base 3 | ||
""" | ||
def ternary_search(l, r, key, arr): | ||
while r >= l: | ||
|
||
mid1 = l + (r-l) // 3 | ||
mid2 = r - (r-l) // 3 | ||
|
||
if key == arr[mid1]: | ||
return mid1 | ||
if key == mid2: | ||
return mid2 | ||
|
||
if key < arr[mid1]: | ||
# key lies between l and mid1 | ||
r = mid1 - 1 | ||
elif key > arr[mid2]: | ||
# key lies between mid2 and r | ||
l = mid2 + 1 | ||
else: | ||
# key lies between mid1 and mid2 | ||
l = mid1 + 1 | ||
r = mid2 - 1 | ||
|
||
# key not found | ||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters