Dynamic Array--Data Structure
来源:互联网 发布:知乎 英国公爵 编辑:程序博客网 时间:2024/06/06 05:54
Three types of Array:
Static:int my_array[100];
Semi-Dynamic: int *my_array = new int[size]; (only if we know the max size)
If the max size is unknown, we use Dynamic Array.
Dynamic Array operations:
Get(i): returns element at location i*.
Set(i, val): Sets element i to Val*.
PushBack(val): Adds Val to the end.
Remove(i): removes element at location i.
Size(): the number of elements.
Dynamic Array Resizing:
If the size of an array is 2,after operation "pushback(a) + pushback(b)". There is no more space at this time。An error will happen if operating "pushback(c)",so we need to create a double size array and copy current data into it, then the operation of "pushback(c)" will be executed。We must create a double size array and copy the current data again when the current array has no space to use. Doing it repeatly until all data has been stored.
Time complexity of Runtime:
Get(i) O(1)
Set(i, val) O(1)
PushBack(val) O(n)
Remove(i) O(n)
Size() O(1)
Common Implementations:
C++ : Vector
JAVA : ArryList
Python: List (the only kind of array)
Three ways to do analysis for amortized cost:
(but nothing change in the code: runtime analysis only)
1, Aggregate method (brute-force sum)
2, Banker's method (tokens )
3, Physicist's method (potential function)
Summary:
1, Unlike static arrays, dynamic arrays can be resized.
2, Appending a new element to a dynamic array is often constant time, but can take O(n).
3, Some space is wasted, but no more than half.
- Dynamic Array--Data Structure
- Array of Data Structure
- USACO2.2 Data Structure&Dynamic Programming&读书笔记
- JavaScript Build Tree Data Structure Using Array
- Data Structure
- Data structure
- Data Structure
- Data structure
- Data structure
- data structure
- Data Structure
- Data structure
- Data Structure
- Data Structure
- Data Structure
- Data Structure
- Data structure alignmentData structure alignment
- abap create dynamic structure and dynamic table
- kotlin学习笔记——sqlite(anko)
- 运维常见命令(二)内存不足报警
- UE4材质与数学计算节点
- 微信公众号支付开发
- 更改状态栏里面图标字体颜色
- Dynamic Array--Data Structure
- 第五章 处理器拦截器详解——跟着开涛学SpringMVC
- beego搭建api服务
- 请求报文及响应报文
- SDK 14 Secure DFU Procedure---nordic 52832空中升级笔记
- OpenSteel v2.30
- iOS数据存储方法大全
- Visual Studio 2010 Win32 Usb HID 动态库创建
- 一个“编码十几年”的『老程序员』分享的四点心得