数组操作的复杂性

编程入门 行业动态 更新时间:2024-10-24 10:28:15
本文介绍了数组操作的复杂性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

因此,我的 comp sci 课程中的主题之一是关于时间复杂度,并使用数组和链表作为比较某些操作的好方法以及哪种容器更擅长这样做,因此您可以选择合适的数据结构.我了解大多数操作背后的原因,但我不确定其中一个是在数组中插入和追加.

So one of the topics in my comp sci class is concerning time complexity and using arrays and linked lists as a good way to compare certain operations and what container is better at doing so, so you can choose the appropiate data structure. I understand the reasoning behind most of the operations but I'm unsure about one and that is inserting and appending in an array.

这两种情况的最坏情况是 O(n).我相信我理解为什么插入是 O(n) 因为最坏的情况是,你在前面插入导致你将所有元素向右移动,这意味着它是线性的并且依赖于数组中的元素数量.对于追加,我很好奇为什么不是 O(1),因为无论大小如何,只要有空间,在最后添加元素都需要一次操作.

The worst case scenario for both of these is O(n). I believe I understand why inserting is O(n) because worst case, you insert at the front causing you to shift all elements over to the right meaning that its linear and dependent on the number of elements in the array. For appending, I was curious why it was not O(1) since it takes one operation no matter the size to add an element at the end, given that there is space.

这是不是问题,如果没有足够的空间,您必须在最坏的情况下将数组复制到更大的数组?

Is that the issue, if there isn't enough space you have to copy the array to a larger one for its worst case scenario?

推荐答案

[...] 如果没有足够的空间,您必须将数组复制到更大的数组最坏的情况?

[...] if there isn't enough space you have to copy the array to a larger one for its worst case scenario?

宾果游戏.

这篇关于数组操作的复杂性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 12:00:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393550.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   复杂性   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!