Binary search with python

The first binary search. 161P of the algorithm picture book written in C language to python in your own way. It will not work if you enter a number that is not in the list. It is assumed that the numbers in the list are arranged in ascending order.

nibun.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

x = [1, 8, 14, 23, 44, 55, 67, 88, 103, 146]
print x
print "Enter the number you want to find"
i = int(raw_input())

low = 0
high = len(x)
#t is the middle number
t = (low + high) / 2 

#Search until the lower limit low of the search becomes the upper limit high
#When low reaches high, the number was not found
while (low<=high):
    if (i==x[t]):
        break
    elif (i > x[t]):
        low = t + 1
    elif (i < x[t]):
        high = t - 1
    t = (low + high) / 2

if (i==x[t]):
    print str(t + 1) + "Is in the second"
else:
    print "There is none"

Recommended Posts

Binary search with python
Binary search with Python3
Sequential search with Python
Binary search in Python
Binary search (python2.7) memo
[Python] Binary search ABC155D
Binary search in Python (binary search)
Algorithm learned with Python 10th: Binary search
Binary search in Python / C ++
Algorithm in Python (binary search)
Search engine work with python
Search twitter tweets with python
Streamline web search with python
Write a binary search in Python
Learn search with Python # 2bit search, permutation search
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
ABC146C (binary search)
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Try working with binary data in Python
Algorithm in Python (ABC 146 C Binary Search
Search the maze with the python A * algorithm
Algorithm learned with Python 12th: Maze search
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Socket communication with Python
Data analysis with python 2
Try scraping with Python.
Automatically search and download YouTube videos with Python
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Run prepDE.py with python3
Causal reasoning and causal search with Python (for beginners)
1.1 Getting Started with Python
Collecting tweets with Python
Python Exercise 1-Breadth-first search
Binarization with OpenCV / Python
Kernel Method with Python
Non-blocking with Python + uWSGI