数据结构算法简介

概述

从结构上看,所有的数据(data)最终都可以分解成三种类型:

  1. 标量(scalar),也就是一个单独的字符串(string)或数字(numbers)
    比如"北京"这个单独的词。
  2. 序列(sequence),也就是若干个相关的数据按照一定顺序并列在一起,又叫做数组(array)或列表(List)
    比如"北京,上海"。
  3. 映射(mapping),也就是一个名/值对(Name/value),即数据有一个名称,还有一个与之相对应的值,这又称作散列(hash)或字典(dictionary)
    比如"首都:北京"。

所以在编程语言中,只要有了数组(array)和对象(object)就能够储存一切数据

常用数据结构与算法

在这里插入图片描述

Abstractdatetype
抽象数据类型
Stack
Queue
队列
Set
Map
Vector
LinkedList
列表
PriorityQueue
优先队列
Heap
HashSet
TreeSet
HashMap
TreeMap

https://blog.csdn.net/xyc1211/article/details/67648424

时间复杂度

O(1): Constant Complexity: Constant 常数复杂度
O(log n): Logarithmic Complexity: 对数复杂度
O(n): Linear Complexity: 线性时间复杂度
O(n2): N square Complexity 平⽅方
O(n3): N square Complexity ⽴立⽅方
O(2n): Exponential Growth 指数
O(n!): Factorial 阶乘

  • O(1)
int n = 1000;
System.out.println("Hey - your input is: " + n);
  • O(N)
for (int = 1; i<=n; i++) {
	System.out.println(“Hey - I'm busy looking at: " + i);
}
  • O(N2)
for (int i = 1; i <= n; i++) {
	for (int j = 1; j <=n; j++) {
		System.out.println("Hey - I'm busy looking at: " + i + " and " + j);
	}
}
  • O(log(n))
for (int i = 1; i < n; i = i * 2) {
	System.out.println("Hey - I'm busy looking at: " + i);
}
  • O(kn)
for (int i = 1; i <= Math.pow(2, n); i++){
	System.out.println("Hey - I'm busy looking at: " + i);
}
  • O(n!)
for (int i = 1; i <= factorial(n); i++){
	System.out.println("Hey - I'm busy looking at: " + i);
}

时间复杂度高的代码,越计算越费时间
在这里插入图片描述

算法的作用

优化程序,降低时间复杂度, 空间复杂度

案例:

实现从1加到n

  • 常见实现方式,时间复杂度为O(N)
y = 0
for i = 1 to n:
	y = i + y
  • 优化为时间复杂度O(1)
y = n * (n + 1) / 2

Master Theorem 主定理

分析算法的时候,我们经常需要分析递归算法的时间复杂度。Master Theorem 正是用于快速得出递归算法时间复杂度的方法。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xyc1211

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值