This is the first day of my participation in the August More Text challenge
Why learn data structures and algorithms
Programmer for three years, feel recently encountered a bottleneck, can only do some surface work, write a script, for example, framework inside to write some to add and delete operation, call some packages, and some API calls, and did some operational work, want to know the source language and its related framework was very demanding, Or going to do in some complicated work (wheels) not sure exactly how to do to do better, confused, feeling the underlying knowledge is not enough, also have their own aspect of knowledge is very wide but not deep, so want to in my head haven’t rust to learn the data structure and algorithm, oneself not seriously system before learning this knowledge, They have always known that this piece of content is very important, has been put it in the back of the consideration, but now consciousness, ha ha, these basic things to take early learn to more quickly understand the rapid learning behind the knowledge ah, can not put the cart before the horse, sand build high platform ~
Heavy learning plan
Let’s start with the simple ones. Let’s start with the simple classic data structures that are commonly used. What programming language do you use? Familiar with Python, Php, Go. I want to try the Java language myself. Not exactly Java, of course, depending on the mood. Of course, data structures and algorithms have nothing to do with programming languages, but are mostly ideas. Of course, it is not according to the knowledge of Baidu or the things in the book to tell again, which will have their own thinking, but also take out the knowledge they have learned to tell a story, and their own experience ~
now
Fibonacci numbers
The first thing you know about algorithms, of course, is calculating the NTH value of the Fibonacci sequence in Python, but let’s go back to the Fibonacci sequence where you start with the third term, and each term is equal to the sum of the first two terms.
Let’s look at using Python to compute the NTH digit of the Fibonacci sequence
Recursive method:
Recursive method, generally speaking, is to call themselves, themselves call themselves, automatically call themselves again, until the call to return, return and return, return and return… Of course, the depth of this call is limited, too deep will report an error. Python can set the depth of recursive function calls with the following code:
import sys
sys.setrecursionlimit(1000000)
Copy the code
Calculate the NTH term of the Fibonacci function column using a recursive method:
import time def fib(n): if n<=1: return n else: Return fib(n-1)+fib(n-2) start_time = time.time() print(fib(35)) end_time = time.time() print(" "% % s seconds (end_time - start_time))Copy the code
Output:
Function execution time: 3.583667516708374 secondsCopy the code
Simple loop:
A method often used by beginners
def fib(n): if n<=1: return n n1 = 0 n2 = 1 for i in range(n-1): N = res return res start_time = time.time() print(fib(35)) end_time = time.time() Print (" function execution time: %s seconds "%(end_time-start_time))Copy the code
Output:
9227465 Function execution time: 0.0 secondsCopy the code
It can be seen that the Fibonacci sequence is computed more slowly using a recursive method than a simple loop method. The shorter the code, the better the performance.
It can be seen that time complexity is an important criterion to evaluate the quality of algorithms.
Time complexity
Time complexity is simply the time it takes to execute code, usually estimated time. In addition to time complexity, there is space complexity, which refers to the amount of temporary memory the code uses during execution. The strength and weakness of an algorithm depends on the execution time of the algorithm and the amount of temporary memory required. Complexity is often described in the big O notation, which represents the complexity of data size N. Complexity can be put simply as data increases as function parameters or code input members increase, and the steeper the execution time curve, the higher the complexity.
conclusion
I’m going to make a quick start today, and then I’m going to talk about my relearning of data structures and algorithms step by step. According to the management of this series of articles will be as popular as possible, ha ha, I hope we point out the shortcomings, mutual progress.