#how does indexing in a dynamic array take O(1) time if you have to iterate through the array?

31 messages · Page 1 of 1 (latest)

fringe pewter
#

basically the title.

fallen harness
#

Dynamic array? If you're iterating it's O(n), it's only O(1) if it's a mapping like in a hash map

fringe pewter
#

So iterating is O(n) but indexing itself is O(1)?

fallen harness
#

Indexing would be O(1) if it's a direct mapping to memory

warm gate
#

When you say "indexing", I assume you mean accessing a single element at a specified index. If not, please correct me. Dynamic arrays are still allocated contiguously in memory. When you go to access an element, that index is treated as a memory offset just as in a statically allocated array.

#

If that is not what you meant, please clarify

fallen harness
#

(it's a bit confusing that you tagged this as "other" and didn't mention a language)

fringe pewter
#

Well it's not really a language. It came up in data structures and algorithms course

fringe pewter
warm gate
fringe pewter
#

Another question, is how did went from n/2 to m/4. I'm not understanding the math here. Because let's say n=6, and after 12/2 removals we resize down the array to 6, how does it become m/4. since 12/4 =3

#

MIT 6.006 Introduction to Algorithms, Spring 2020
Instructor: Jason Ku
View the complete course: https://ocw.mit.edu/6-006S20
YouTube Playlist: https://www.youtube.com/playlist?list=PLUl4u3cNGP63EdVPNLG3ToM6LaEUuStEY

Four examples of worked problems on the asymptotic behavior of functions and double-ended sequence operations.

License: Creative...

▶ Play video
devout fulcrum
#

They defined m to be 2n

#

So n/2 = m/4

#

I think

warm gate
#

that's what I took from it lol

devout fulcrum
warm gate
devout fulcrum
#

Agreed

fringe pewter
devout fulcrum
#

I think so, but I think it also depends on the growth factor

#

Don't quote me on any of this, I never actually properly learned this stuff lol

warm gate
#

I would have to watch through more of that video for more context on the things he's actually referring to, but I don't have time to do so at the moment

devout fulcrum
#

Yeah neither do I

fringe pewter
#

he doesn't really go more in detail. the next few minutes was just another way to create a data structure that can have O(1) indexing and amortized O(1) insert/deletion

warm gate
#

I think a lot of data structures are more practical to learn in the context of algorithms they're useful in

#

an array is a very primitive type so learning it generally makes more sense

#

but there are a lot of other data types that are good for very specific cases

fringe pewter
#

i haven't gotten into any algorithms yet. I just learned about ADTs like a list and set and then we finally got into the data structures like linked list and dyanmic array