Algorithm learned with Python 10th: Binary search

#Algorithm learned in Python

Introduction

Implement the basic algorithm in Python to deepen your understanding of the algorithm. Binary search is dealt with as the 10th bullet.

Binary search

When you look at a value, such as the median, you determine whether that value is before or after it. ・ The search range is narrowed by half (divided into two) ⇒ Speed ​​up ※Note ** Data must be arranged regularly, such as in alphabetical order ** In some cases, it may be necessary to sort the data in advance.

Difference from linear search

The order is as follows. Linear search: $ O (n) $ Binary search: $ O (log_2n) $ The relationship is shown in a graph for a more intuitive understanding. image.png

For example, if there are 1000 or 1 million pieces of data, a linear search requires 1000 or 1 million comparisons, but a binary search requires only 10 or 20 comparisons.

Although there is a restriction that the data must be arranged regularly, it can be seen that the binary search is effective when the amount of data is large. However, when there is little data, there is no big difference in speed, so a linear search that is not related to the data arrangement is often used.

Implementation

Two implementations are made for binary search. The first is when data that is regularly arranged is given, and the second is that it can be sorted, but it is assumed that it is arranged irregularly and needs to be sorted. The code and its output are shown below.

Code 1

binary_search.py


"""
2020/12/22
@Yuya Shimizu

Binary search
"""

def binary_search(data, target):
    left = 0                    #Set the left edge of the search range
    right = len(data) - 1       #Set the right edge of the search range

    while left <= right:
        mid =  (left + right) // 2         #Median range to search
        if data[mid] == target:
            return mid                     #Returns the position if it matches the median
        elif data[mid] < target:
            left = mid + 1                 #If it is larger than the median, change the left edge of the search range.
        else:
            right = mid - 1                #If it is smaller than the median, change the right edge of the search range.

    return False


if __name__ == '__main__':
    data = [10, 20, 30, 40, 50, 60, 70, 80, 90]
    target = 5
    
    found = binary_search(data, target)
    if not found:
        print(f"{target} is not found in the data")
    else:
        print(f"{target} is found at data[{found}]")
Output (when data is found)
50 is found at data[4]
Output (when no data is found)
5 is not found in the data
Code 2

binary_search.py


"""
2020/12/22
@Yuya Shimizu

Binary search
"""

def binary_search(data, target):
    data_sorted = sorted(data)  #Sort the data in ascending order(data, reverse=True)Then you can do it in descending order
    
    left = 0                    #Set the left edge of the search range
    right = len(data) - 1       #Set the right edge of the search range

    while left <= right:
        mid =  (left + right) // 2         #Median range to search
        if data_sorted[mid] == target:
            return mid , data_sorted       #If it matches the median, the data sorted by that position is returned.
        elif data_sorted[mid] < target:
            left = mid + 1                 #If it is larger than the median, change the left edge of the search range.
        else:
            right = mid - 1                #If it is smaller than the median, change the right edge of the search range.

    return False, -1


if __name__ == '__main__':
    data = [10, 50, 60, 20, 30, 40, 90, 70, 80]
    target = 50
    
    found, data_sorted = binary_search(data, target)

    if not found:
        print(f"{target} is not found in the data")
    else:
        print(f"data_sorted is the sorted data : \n{data} >>> {data_sorted}\n")
        print(f"{target} is found at data_sorted[{found}]")
Output (when data is found)
data_sorted is the sorted data : 
[10, 50, 60, 20, 30, 40, 90, 70, 80] >>> [10, 20, 30, 40, 50, 60, 70, 80, 90]

50 is found at data_sorted[4]
Output (when no data is found)
5 is not found in the data

I don't know if the second one is necessary, but I tried to make it like this if I made a function that also includes sorting. I think it will work if it is a sortable data list. The search range is reduced by repeatedly replacing the left and right edges with the median.

Impressions

I learned that there is such a difference from linear search. Although it is natural from the mechanism, I was able to understand it more intuitively by displaying it in a graph.

References

Introduction to algorithms starting with Python: Standards and computational complexity learned with traditional algorithms Written by Toshikatsu Masui, Shoeisha

Recommended Posts

Algorithm learned with Python 10th: Binary search
Algorithm learned with Python 9th: Linear search
Algorithm learned with Python 12th: Maze search
Binary search with python
Binary search with Python3
Algorithm learned with Python 5th: Fibonacci sequence
Algorithm learned with Python 7th: Year conversion
Algorithm learned with Python 8th: Evaluation of algorithm
Algorithm learned with Python 4th: Prime numbers
Algorithm learned with Python 19th: Sorting (heapsort)
Algorithm learned with Python 6th: Leap year
Algorithm learned with Python 11th: Tree structure
Algorithm learned with Python 13th: Tower of Hanoi
Algorithm learned with Python 16th: Sorting (insertion sort)
Algorithm learned with Python 14th: Tic-tac-toe (ox problem)
Algorithm learned with Python 15th: Sorting (selection sort)
Algorithm in Python (binary search)
Algorithm learned with Python 17th: Sorting (bubble sort)
Algorithm learned with Python 18th: Sorting (stack and queue)
Algorithm in Python (ABC 146 C Binary Search
Search the maze with the python A * algorithm
Algorithm learned with Python 2nd: Vending machine
Algorithm learned with Python 3rd: Radix conversion
Sequential search with Python
Binary search in Python
Binary search (python2.7) memo
[Python] Binary search ABC155D
Binary search in Python (binary search)
Search algorithm using word2vec [python]
Binary search in Python / C ++
Full bit search with Python
[Python3] Dijkstra's algorithm with 14 lines
Search engine work with python
Search twitter tweets with python
Streamline web search with python
Algorithm in Python (breadth-first search, bfs)
Write a binary search in Python
[Python] Object-oriented programming learned with Pokemon
Learn search with Python # 2bit search, permutation search
Algorithm in Python (depth-first search, dfs)
Perceptron learning experiment learned with Python
Python data structures learned with chemoinformatics
Efficient net pick-up learned with Python
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
[C language algorithm] Binary search tree
Implementation of Dijkstra's algorithm with python
Python algorithm
Try working with binary data in Python
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Let's develop an investment algorithm with Python 1
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
FizzBuzz with Python3
Csv output from Google search with [Python]! 【Easy】
Automatically search and download YouTube videos with Python
Scraping with Python
Statistics with python
Causal reasoning and causal search with Python (for beginners)
Python memorandum (algorithm)
Scraping with Python
Python with Go
Twilio with Python