mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
Simplify code by dropping support for legacy Python (#1143)
* Simplify code by dropping support for legacy Python * sort() --> sorted()
This commit is contained in:
@ -1,20 +1,13 @@
|
||||
'''
|
||||
This is a type of divide and conquer algorithm which divides the search space into
|
||||
3 parts and finds the target value based on the property of the array or list
|
||||
3 parts and finds the target value based on the property of the array or list
|
||||
(usually monotonic property).
|
||||
|
||||
Time Complexity : O(log3 N)
|
||||
Space Complexity : O(1)
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
import sys
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
# This is the precision for this function which can be altered.
|
||||
# It is recommended for users to keep this number greater than or equal to 10.
|
||||
precision = 10
|
||||
@ -31,23 +24,23 @@ def ite_ternary_search(A, target):
|
||||
right = len(A) - 1;
|
||||
while(True):
|
||||
if(left<right):
|
||||
|
||||
|
||||
if(right-left < precision):
|
||||
return lin_search(left,right,A,target)
|
||||
|
||||
oneThird = (left+right)/3+1;
|
||||
twoThird = 2*(left+right)/3+1;
|
||||
|
||||
|
||||
if(A[oneThird] == target):
|
||||
return oneThird
|
||||
elif(A[twoThird] == target):
|
||||
return twoThird
|
||||
|
||||
|
||||
elif(target < A[oneThird]):
|
||||
right = oneThird-1
|
||||
elif(A[twoThird] < target):
|
||||
left = twoThird+1
|
||||
|
||||
|
||||
else:
|
||||
left = oneThird+1
|
||||
right = twoThird-1
|
||||
@ -57,7 +50,7 @@ def ite_ternary_search(A, target):
|
||||
# This is the recursive method of the ternary search algorithm.
|
||||
def rec_ternary_search(left, right, A, target):
|
||||
if(left<right):
|
||||
|
||||
|
||||
if(right-left < precision):
|
||||
return lin_search(left,right,A,target)
|
||||
|
||||
@ -68,12 +61,12 @@ def rec_ternary_search(left, right, A, target):
|
||||
return oneThird
|
||||
elif(A[twoThird] == target):
|
||||
return twoThird
|
||||
|
||||
|
||||
elif(target < A[oneThird]):
|
||||
return rec_ternary_search(left, oneThird-1, A, target)
|
||||
elif(A[twoThird] < target):
|
||||
return rec_ternary_search(twoThird+1, right, A, target)
|
||||
|
||||
|
||||
else:
|
||||
return rec_ternary_search(oneThird+1, twoThird-1, A, target)
|
||||
else:
|
||||
@ -87,7 +80,7 @@ def __assert_sorted(collection):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
user_input = raw_input('Enter numbers separated by coma:\n').strip()
|
||||
user_input = input('Enter numbers separated by coma:\n').strip()
|
||||
collection = [int(item) for item in user_input.split(',')]
|
||||
|
||||
try:
|
||||
@ -95,11 +88,11 @@ if __name__ == '__main__':
|
||||
except ValueError:
|
||||
sys.exit('Sequence must be sorted to apply the ternary search')
|
||||
|
||||
target_input = raw_input('Enter a single number to be found in the list:\n')
|
||||
target_input = input('Enter a single number to be found in the list:\n')
|
||||
target = int(target_input)
|
||||
result1 = ite_ternary_search(collection, target)
|
||||
result2 = rec_ternary_search(0, len(collection)-1, collection, target)
|
||||
|
||||
|
||||
if result2 is not None:
|
||||
print('Iterative search: {} found at positions: {}'.format(target, result1))
|
||||
print('Recursive search: {} found at positions: {}'.format(target, result2))
|
||||
|
Reference in New Issue
Block a user