Data Structures Series

本文对比了数组、List、Collection、LinkedList、Stack、Queue及Dictionary等数据结构在执行常见操作如添加、删除、随机访问等时的时间复杂度。通过这些信息可以帮助开发者选择最适合其应用场景的数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Cheat Sheet: Speed of Common Operations

add to end

remove
from end

insert at middle

remove
from middle

Random Access

In-order
Access

Search for
specific element

Notes

Array

O(n)

O(n)

O(n)

O(n)

O(1)

O(1)

O(n)

Most efficient use of memory; use in cases where data size is fixed.

List<T>

best case O(1); worst case O(n)

O(1)

O(n)

O(n)

O(1)

O(1)

O(n)

Implementation is optimized
for speed. In many cases, List
will be the best choice.

Collection<T>

best case O(1); worst case O(n)

O(1)

O(n)

O(n)

O(1)

O(1)

O(n)

List is a better choice, unless
publicly exposed as API.

LinkedList<T>

O(1)

O(1)

O(1)

O(1)

O(n)

O(1)

O(n)

Many operations are fast,
but watch out for cache
coherency.

Stack<T>

best case O(1); worst case O(n)

O(1)

N/A

N/A

N/A

N/A

N/A

Shouldn't be selected for
performance reasons, but
algorithmic ones.

Queue<T>

best case O(1); worst case O(n)

O(1)

N/A

N/A

N/A

N/A

N/A

Shouldn't be selected for
performance reasons, but
algorithmic ones.

Dictionary<K,T>

best case O(1); worst case O(n)

O(1)

best case O(1); worst case O(n)

O(1)

O(1)*

O(1)*

O(1)

Although in-order access time
is constant time, it is usually slower than other structures due to the over-head of looking up the key.

 

转载于:https://www.cnblogs.com/jerryhong/articles/1064190.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值