mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
update 'sorted' to 'ascending sorted' in comments (#789)
To avoid confusion all 'sorted' to 'ascending sorted' in comments
This commit is contained in:
parent
e22ea7e380
commit
7677c37011
|
@ -21,10 +21,10 @@ except NameError:
|
||||||
def binary_search(sorted_collection, item):
|
def binary_search(sorted_collection, item):
|
||||||
"""Pure implementation of binary search algorithm in Python
|
"""Pure implementation of binary search algorithm in Python
|
||||||
|
|
||||||
Be careful collection must be sorted, otherwise result will be
|
Be careful collection must be ascending sorted, otherwise result will be
|
||||||
unpredictable
|
unpredictable
|
||||||
|
|
||||||
:param sorted_collection: some sorted collection with comparable items
|
:param sorted_collection: some ascending sorted collection with comparable items
|
||||||
:param item: item value to search
|
:param item: item value to search
|
||||||
:return: index of found item or None if item is not found
|
:return: index of found item or None if item is not found
|
||||||
|
|
||||||
|
@ -60,10 +60,10 @@ def binary_search(sorted_collection, item):
|
||||||
def binary_search_std_lib(sorted_collection, item):
|
def binary_search_std_lib(sorted_collection, item):
|
||||||
"""Pure implementation of binary search algorithm in Python using stdlib
|
"""Pure implementation of binary search algorithm in Python using stdlib
|
||||||
|
|
||||||
Be careful collection must be sorted, otherwise result will be
|
Be careful collection must be ascending sorted, otherwise result will be
|
||||||
unpredictable
|
unpredictable
|
||||||
|
|
||||||
:param sorted_collection: some sorted collection with comparable items
|
:param sorted_collection: some ascending sorted collection with comparable items
|
||||||
:param item: item value to search
|
:param item: item value to search
|
||||||
:return: index of found item or None if item is not found
|
:return: index of found item or None if item is not found
|
||||||
|
|
||||||
|
@ -89,11 +89,11 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
|
||||||
|
|
||||||
"""Pure implementation of binary search algorithm in Python by recursion
|
"""Pure implementation of binary search algorithm in Python by recursion
|
||||||
|
|
||||||
Be careful collection must be sorted, otherwise result will be
|
Be careful collection must be ascending sorted, otherwise result will be
|
||||||
unpredictable
|
unpredictable
|
||||||
First recursion should be started with left=0 and right=(len(sorted_collection)-1)
|
First recursion should be started with left=0 and right=(len(sorted_collection)-1)
|
||||||
|
|
||||||
:param sorted_collection: some sorted collection with comparable items
|
:param sorted_collection: some ascending sorted collection with comparable items
|
||||||
:param item: item value to search
|
:param item: item value to search
|
||||||
:return: index of found item or None if item is not found
|
:return: index of found item or None if item is not found
|
||||||
|
|
||||||
|
@ -123,11 +123,11 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
|
||||||
return binary_search_by_recursion(sorted_collection, item, midpoint+1, right)
|
return binary_search_by_recursion(sorted_collection, item, midpoint+1, right)
|
||||||
|
|
||||||
def __assert_sorted(collection):
|
def __assert_sorted(collection):
|
||||||
"""Check if collection is sorted, if not - raises :py:class:`ValueError`
|
"""Check if collection is ascending sorted, if not - raises :py:class:`ValueError`
|
||||||
|
|
||||||
:param collection: collection
|
:param collection: collection
|
||||||
:return: True if collection is sorted
|
:return: True if collection is ascending sorted
|
||||||
:raise: :py:class:`ValueError` if collection is not sorted
|
:raise: :py:class:`ValueError` if collection is not ascending sorted
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> __assert_sorted([0, 1, 2, 4])
|
>>> __assert_sorted([0, 1, 2, 4])
|
||||||
|
@ -136,10 +136,10 @@ def __assert_sorted(collection):
|
||||||
>>> __assert_sorted([10, -1, 5])
|
>>> __assert_sorted([10, -1, 5])
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: Collection must be sorted
|
ValueError: Collection must be ascending sorted
|
||||||
"""
|
"""
|
||||||
if collection != sorted(collection):
|
if collection != sorted(collection):
|
||||||
raise ValueError('Collection must be sorted')
|
raise ValueError('Collection must be ascending sorted')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
__assert_sorted(collection)
|
__assert_sorted(collection)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
sys.exit('Sequence must be sorted to apply binary search')
|
sys.exit('Sequence must be ascending sorted to apply binary search')
|
||||||
|
|
||||||
target_input = raw_input('Enter a single number to be found in the list:\n')
|
target_input = raw_input('Enter a single number to be found in the list:\n')
|
||||||
target = int(target_input)
|
target = int(target_input)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user