当前位置:网站首页>时间复杂度
时间复杂度
2022-06-23 00:39:00 【恒恒恒咩】
时间复杂度
1.常数时间操作
一个操作如果和样本的数据量没有关系,每次都是固定时间内完成的操作,这个操作叫做常数操作。
例子:int a=arr[i]; a将i位置的数取出,是一个固定的时间,与arr的数据量没有关系,这就是一个常数操作。
反之,int b =list.get[i];是一个链表,b取值需要从链表的开头一个一个取,i在第几千位,就要查看多少位,跟数据量有关,不是一个常数操作。
加减乘除,位运算等都属于常数操作。
2.时间复杂度
时间复杂度是一个算法流程中,常数操作数量的一个指标。常用o表示,读作big o。
具体来说,一个算法流程中进行了多少次常数操作(用N表示),总结出常数操作的数量表达式(N的表达式),只留下最高阶项,并且不要最高阶项的系数,剩下的部分如果为f(N),那么时间复杂度为o(f(N))。
3.评价算法流程好坏
评价一个算法流程好坏,先看时间复杂度指标,最高阶项数。如果相同,再分析不同数据样本下实际运行时间,也就是“常数项时间”。(就是看N表达式的系数,假如你N1是10,N2是100,但是N2的100中有很多位运算,N1的10中是加减乘除,无法直接因为10<100就说N1算法更好,因为加减乘除与位运算的常数操作时间不同,不确定,需要到实际情况下去实验哪个更好。)
遇到指标相同时,直接用实验的方式,将代码分别运行进行判断即可。
例子:
` public static void text1(){
int N =10000;
int a=0;
for(int i=0;i<N;i++){
a=12+100;
a=32*12;`
}
}
`public static void text2(){``
int N =10000;
int a=0;
for(int i=0;i<N;i++){
a=12|100;
a=746^12;
}
}
text1和text2的时间复杂度的指标相同都为N,接下来直接代码测试进行判断哪个更好,不用去嗯比系数。
边栏推荐
- Shell 查看帮助
- Big guys, 2.2.1 the CDC monitoring SQLSEVER can only get the full amount of data. Why can't we get the incremental data in the later period
- 权限想要细化到按钮,怎么做?
- 層次選擇器
- Ros2 summer school 2022 transfer-
- Node fetch download file
- Add expiration time for localstorage
- MGRE环境下的OSPF实验
- “听觉”营销价值凸显,喜马拉雅迎来新局点
- 通过天天基金投资基金安全吗?我打算开户买基金
猜你喜欢

How to solve the problem that easycvr does not display the interface when RTMP streaming is used?

权限想要细化到按钮,怎么做?

Cadence spb17.4 - Chinese UI settings

【机器学习-西瓜书】更文挑战【Day1】:1.1 引言

A hundred lines of code to realize reliable delay queue based on redis

Dig three feet to solve the data consistency problem between redis and MySQL

Explain the startup process of opengauss multithreading architecture in detail

Daily question brushing record (I)

SAP MM ME27 创建公司内STO单

黄金etf持仓量如何算
随机推荐
Which brokerage platform is better and safer for a brokerage to open an account on a mobile phone? What if you need a low commission
LINQ 查詢
Shell 查看帮助
百度交易中台之钱包系统架构浅析
LeetCode刷题——715. Range 模块
如何入门机器学习?
Explain the startup process of opengauss multithreading architecture in detail
Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
leetcode 91. Decode Ways 解码方法(中等)
Psychological analysis of the safest spot Silver
Is it safe to open a new bond? How
New progress in the construction of meituan's Flink based real-time data warehouse platform
Typecho仿盧松松博客主題模板/科技資訊博客主題模板
Steps to implement a container global component
The longest child sequence of the 2019 Blue Bridge Cup
Ansible learning summary (8) -- Summary of ansible control right raising related knowledge
[launch] redis Series 2: data persistence to improve availability
Figure what are the uses and applications of neural networks?
中金证券开户安全吗?它和中金银行是什么关系呢?
leetcode 91. Decode ways (medium)