Algorithm learned with Python 9th: Linear search

#Algorithm learned in Python

Introduction

Implement the basic algorithm in Python to deepen your understanding of the algorithm. As the ninth bullet, we deal with linear search.

Linear search

Search: Finding the data you want from a lot of data Linear: Examine in order from the beginning

In other words, linear search does not do anything complicated, but searches from the front in order.

Programmatic linear search

Store the data in the list and check it in order from the beginning. This is a very simple program structure and easy to implement ⇒ ** Effective method when the number of data is small **

Implementation

The code of the linear search and the output at that time are shown below.

code

linear_search.py


"""
2020/12/21
@Yuya Shimizu

Linear search
"""
#Linear search function
def linear_search(data, target):
    for i in range(len(data)):
        if data[i] == target:
            return i
    return False


if __name__ == '__main__':
    data = [50, 20, 70, 60, 40, 90, 30] #Target data to be searched
    target = 40  #Value to search
    found = linear_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
40 is found at data[4]

Next, the output result when False is returned is also shown.

output
45 is not found in the data

A brief description of the linear search function

It is turned by the for statement, but since we want to return the index of the data as the return value, we dare to usefor i in range (len (data)). Also, by return in for, it can be expressed by simply writing return False outside the for statement that the search result cannot be found.

Impressions

Linear search is very simple and overlaps with intuition. I think it will be the basis for other searches. I am looking forward to learning from now on.

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 9th: Linear search
Algorithm learned with Python 10th: Binary search
Algorithm learned with Python 12th: Maze search
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 learned with Python 17th: Sorting (bubble sort)
Algorithm learned with Python 18th: Sorting (stack and queue)
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
Linear search in Python
Binary search with python
Binary search with Python3
Search algorithm using word2vec [python]
Algorithm in Python (binary search)
Full bit search with Python
[Python3] Dijkstra's algorithm with 14 lines
[Python] Linear regression with scikit-learn
Search engine work with python
Search twitter tweets with python
Streamline web search with python
[Python] Object-oriented programming learned with Pokemon
Learn search with Python # 2bit search, permutation search
Algorithm in Python (depth-first search, dfs)
Master linear search! ~ Python implementation version ~
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)
Implementation of Dijkstra's algorithm with python
Python algorithm
[Python] Reactive Extensions learned with RxPY (3.0.1) [Rx]
Basic Linear Algebra Learned in Python (Part 1)
Algorithm in Python (ABC 146 C Binary Search
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
Automatically search and download YouTube videos with Python
Scraping with Python
Statistics with python
Let's solve simultaneous linear equations with Python sympy!
Causal reasoning and causal search with Python (for beginners)
Python memorandum (algorithm)
Scraping with Python
[Fundamental Information Technology Engineer Examination] I wrote a linear search algorithm in Python.
Python with Go
Optimization learned with OR-Tools [Linear programming: multi-stage model]
Twilio with Python
Integrate with Python
Play with 2016-Python