|
1 |
| -def binarySearch(alist, item): |
2 |
| - |
3 |
| -first = 0 |
4 |
| -last = len(alist)-1 |
5 |
| -found = False |
6 |
| - |
7 |
| -while first<=last and not found: |
8 |
| - |
9 |
| -midpoint = (first + last)//2 |
10 |
| -if alist[midpoint] == item: |
11 |
| -found = True |
12 |
| -print("Found [ at position: %s ]" % (alist.index(item) + 1)) |
| 1 | +""" |
| 2 | +This is pure python implementation of binary search algorithm |
| 3 | +
|
| 4 | +For doctests run following command: |
| 5 | +python -m doctest -v selection_sort.py |
| 6 | +or |
| 7 | +python3 -m doctest -v selection_sort.py |
| 8 | +
|
| 9 | +For manual testing run: |
| 10 | +python binary_search.py |
| 11 | +""" |
| 12 | +from __future__ import print_function |
| 13 | + |
| 14 | + |
| 15 | +def binary_search(sorted_collection, item): |
| 16 | +"""Pure implementation of binary sort algorithm in Python |
| 17 | +
|
| 18 | +:param sorted_collection: some sorted collection with comparable items |
| 19 | +:param item: item value to search |
| 20 | +:return: index of found item or None if item is not found |
| 21 | +
|
| 22 | +Examples: |
| 23 | +>>> binary_search([0, 5, 7, 10, 15], 0) |
| 24 | +0 |
| 25 | +
|
| 26 | +>>> binary_search([0, 5, 7, 10, 15], 15) |
| 27 | +4 |
| 28 | +
|
| 29 | +>>> binary_search([0, 5, 7, 10, 15], 5) |
| 30 | +1 |
| 31 | +
|
| 32 | +>>> binary_search([0, 5, 7, 10, 15], 6) |
| 33 | +
|
| 34 | +>>> binary_search([5, 2, 1, 5], 2) |
| 35 | +Traceback (most recent call last): |
| 36 | +... |
| 37 | +ValueError: Collection must be sorted |
| 38 | +""" |
| 39 | +if sorted_collection != sorted(sorted_collection): |
| 40 | +raise ValueError('Collection must be sorted') |
| 41 | +left = 0 |
| 42 | +right = len(sorted_collection) - 1 |
| 43 | + |
| 44 | +while left <= right: |
| 45 | +midpoint = (left + right) // 2 |
| 46 | +current_item = sorted_collection[midpoint] |
| 47 | +if current_item == item: |
| 48 | +return midpoint |
13 | 49 | else:
|
14 |
| - |
15 |
| -if item < alist[midpoint]: |
16 |
| - |
17 |
| -last = midpoint-1 |
| 50 | +if item < current_item: |
| 51 | +right = midpoint - 1 |
18 | 52 | else:
|
19 |
| -first = midpoint+1 |
20 |
| - |
21 |
| -if found == False: |
22 |
| -continue |
23 |
| -# print("Not found") |
24 |
| -return found |
25 |
| -print("Enter numbers seprated by space") |
26 |
| -s = input() |
27 |
| -numbers = list(map(int, s.split())) |
28 |
| -trgt =int( input('enter a single number to be found in the list ')) |
29 |
| -binarySearch(numbers, trgt) |
| 53 | +left = midpoint + 1 |
| 54 | +return None |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | +import sys |
| 59 | +# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin |
| 60 | +# otherwise 2.x's input builtin function is too "smart" |
| 61 | +if sys.version_info.major < 3: |
| 62 | +input_function = raw_input |
| 63 | +else: |
| 64 | +input_function = input |
| 65 | + |
| 66 | +user_input = input_function('Enter numbers separated by coma:\n') |
| 67 | +collection = [int(item) for item in user_input.split(',')] |
30 | 68 |
|
| 69 | +target_input = input_function( |
| 70 | +'Enter a single number to be found in the list:\n' |
| 71 | +) |
| 72 | +target = int(target_input) |
| 73 | +result = binary_search(collection, target) |
| 74 | +if result is not None: |
| 75 | +print('{} found at positions: {}'.format(target, result)) |
| 76 | +else: |
| 77 | +print('Not found') |
0 commit comments