21
21
def binary_search (sorted_collection , item ):
22
22
"""Pure implementation of binary search algorithm in Python
23
23
24
- Be careful collection must be sorted, otherwise result will be
24
+ Be careful collection must be ascending sorted, otherwise result will be
25
25
unpredictable
26
26
27
- :param sorted_collection: some sorted collection with comparable items
27
+ :param sorted_collection: some ascending sorted collection with comparable items
28
28
:param item: item value to search
29
29
:return: index of found item or None if item is not found
30
30
@@ -60,10 +60,10 @@ def binary_search(sorted_collection, item):
60
60
def binary_search_std_lib (sorted_collection , item ):
61
61
"""Pure implementation of binary search algorithm in Python using stdlib
62
62
63
- Be careful collection must be sorted, otherwise result will be
63
+ Be careful collection must be ascending sorted, otherwise result will be
64
64
unpredictable
65
65
66
- :param sorted_collection: some sorted collection with comparable items
66
+ :param sorted_collection: some ascending sorted collection with comparable items
67
67
:param item: item value to search
68
68
:return: index of found item or None if item is not found
69
69
@@ -89,11 +89,11 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
89
89
90
90
"""Pure implementation of binary search algorithm in Python by recursion
91
91
92
- Be careful collection must be sorted, otherwise result will be
92
+ Be careful collection must be ascending sorted, otherwise result will be
93
93
unpredictable
94
94
First recursion should be started with left=0 and right=(len(sorted_collection)-1)
95
95
96
- :param sorted_collection: some sorted collection with comparable items
96
+ :param sorted_collection: some ascending sorted collection with comparable items
97
97
:param item: item value to search
98
98
:return: index of found item or None if item is not found
99
99
@@ -123,11 +123,11 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
123
123
return binary_search_by_recursion (sorted_collection , item , midpoint + 1 , right )
124
124
125
125
def __assert_sorted (collection ):
126
- """Check if collection is sorted, if not - raises :py:class:`ValueError`
126
+ """Check if collection is ascending sorted, if not - raises :py:class:`ValueError`
127
127
128
128
:param collection: collection
129
- :return: True if collection is sorted
130
- :raise: :py:class:`ValueError` if collection is not sorted
129
+ :return: True if collection is ascending sorted
130
+ :raise: :py:class:`ValueError` if collection is not ascending sorted
131
131
132
132
Examples:
133
133
>>> __assert_sorted([0, 1, 2, 4])
@@ -136,10 +136,10 @@ def __assert_sorted(collection):
136
136
>>> __assert_sorted([10, -1, 5])
137
137
Traceback (most recent call last):
138
138
...
139
- ValueError: Collection must be sorted
139
+ ValueError: Collection must be ascending sorted
140
140
"""
141
141
if collection != sorted (collection ):
142
- raise ValueError ('Collection must be sorted' )
142
+ raise ValueError ('Collection must be ascending sorted' )
143
143
return True
144
144
145
145
@@ -150,7 +150,7 @@ def __assert_sorted(collection):
150
150
try :
151
151
__assert_sorted (collection )
152
152
except ValueError :
153
- sys .exit ('Sequence must be sorted to apply binary search' )
153
+ sys .exit ('Sequence must be ascending sorted to apply binary search' )
154
154
155
155
target_input = raw_input ('Enter a single number to be found in the list:\n ' )
156
156
target = int (target_input )
0 commit comments