当前位置:网站首页>Dear students, don't read the textbooks any more. Just read this one for the complexity of time
Dear students, don't read the textbooks any more. Just read this one for the complexity of time
2022-06-22 15:57:00 【m0_ sixty million seven hundred and twenty-one thousand five hu】
Time complexity is the cornerstone of learning algorithms , Today, let's talk about why we should introduce time complexity , What is the time complexity and how to calculate the time complexity of an algorithm
One 、 Describe the running time of the algorithm
One day , Huineng called Yichen to teach him some basic knowledge , I saw that G wrote a very simple code








Yichen sees that the teacher is a little angry , I began to ask for advice with an open mind



For the convenience of discussion , Here we regard the execution time of each statement as the same , As a Time unit



① The two statements in the blue box , Take two time units
② A statement with a black box , cost n+1 Time units
③ The two statements in the red box , cost 2*n Time units

Isn't this math , Yichen thought to himself

Among them n What we call the scale of the problem , In fact, it is the size of the problem you deal with
Huineng drew the graph of this function conveniently

This paper mainly discusses the relationship between problem size and running time , It is assumed that the different inputs are basically independent of the running time





Two 、 Time complexity

for instance :T(n)=3n+3, When n A very large Time constant 3 and n The coefficient of 3 The effect on the result of the function is very small


such as :
T(n)=n+1 Ignore the constant term T(n)~n
T(n)=n+n^2 Ignore lower order terms T(n)~n^2
T(n)=3n Ignore the coefficient of the highest order T(n)~n





Fortunately, I don't have to master the headache of mathematics , Yichen thought

Yichen brings the topic back



More precisely O Represents an asymptotic upper bound of the runtime function , namely T(n) Less than or equal to in order of magnitude f(n)

3、 ... and 、 Calculation of time complexity

One 、 Get the function of running time Two 、 Simplify the function
① With constant 1 To replace all addition constants in run time
② In the modified function , Only the highest order terms ③ If the highest order term exists and is not 1, The coefficient of this term is ignored



O(1) Also known as constant order





Yichen wrote a piece of code with nested loops





next , Huineng wrote another period of code with logarithmic complexity



Yichen, who is not good at mathematics, is a little confused at this time







summary
Learning algorithms , The first step is to know what time complexity is , What is spatial complexity , In fact, you understand the complexity of time , So you can understand the spatial complexity , It is suggested that you still be schoolmates , We must learn the course of data structure and algorithm well .
Whether it's an interview or improving your content , Algorithm learning is essential , I would like to recommend a certain BAT The boss summed it up Leetcode Brush notes :BAT It's classified and summarized by the boss Leetcode Brush the topic template , Help you with 90% Interview
in addition , I have also written a series of articles on sorting algorithms in cartoons , Post a link directly here , I wish you were in charge of the collection , Hey . But here is only 7 A sort algorithm that must be mastered , Like bucket sort , Cardinality sort these , Understanding can , It will also be written in the later stage .
comic : What is bubble sorting algorithm ?
comic : What is a selective sorting algorithm ?
comic : What is the insertion sort algorithm ?
comic : What is hill sorting algorithm ?
comic : What is a merge sort algorithm ?
comic : What is a quick sort algorithm ?
comic : What is a heap sorting algorithm ?
Of course , You are also welcome to play on your personal website :https://www.iamshuaidi.com, from 0 To 1 Summed up personal learning .
The author is concise
author : Hello everyone , I am a , From University 、 Self study all the way , Know perfectly well Algorithm , Basic computer knowledge Importance , official account 「 Play programming 」10 Million fans, author , Specialized in writing these basic knowledge , Improve our internal skills , Looking forward to your attention , Study with me , Click on I've been in College for four years The road of learning Reprint note : Not authorized , Prohibited reproduced
边栏推荐
- Application of mongodb in Tencent retail premium code
- Discourse 新用户可插入媒体的数量
- Batch export excel zip using zipfile, openpyxl and flask
- 还可以这样搞nlp(分类)
- [译文] 弥合开源数据库和数据库业务之间的鸿沟
- Trust level of discover
- Scala language learning-05-a comparison of the efficiency of recursion and tail recursion
- How to use the concat() function of MySQL
- 小白操作Win10扩充C盘(把D盘内存分给C盘)亲测多次有效
- stack和queue的模拟实现
猜你喜欢

小白操作Win10扩充C盘(把D盘内存分给C盘)亲测多次有效

Meet webassembly again

Fast and accurate point cloud registration based on minimizing 3D NDT distance
![[Newman] postman generates beautiful test reports](/img/5c/b95c1c475e69d69acad75215ea9565.png)
[Newman] postman generates beautiful test reports

壹连科技冲刺深交所:年营收14亿 65%收入来自宁德时代

再次认识 WebAssembly

Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe

建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!

84.(cesium篇)cesium模型在地形上运动

Community article | mosn building subset optimization ideas sharing
随机推荐
建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!
Applet development - Custom expiration cache
vector的模拟实现
Quickly play ci/cd graphical choreography
stack和queue的模拟实现
DevSecOps: CI/CD 流水线安全的最佳实践
【华为云至简致远】征文获奖名单出炉!
Rosbag use command
华为机器学习服务银行卡识别功能,一键实现银行卡识别与绑定
社区文章|MOSN 构建 Subset 优化思路分享
向量1(类和对象)
宏源期货开户安全么?宏源期货公司可以降低手续费?
做自媒体视频博主,必备的32个素材网站分享
C language learning -17- function is passed in as a parameter
【LeetCode】9、回文数
Scala语言学习-06-传名参数、传值参数、传函数参数的区别
[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want
鸿世电器冲刺创业板:年营收6亿 刘金贤股权曾被广德小贷冻结
程序替换函数
Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!