#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)
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
So iterating is O(n) but indexing itself is O(1)?
Indexing would be O(1) if it's a direct mapping to memory
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
(it's a bit confusing that you tagged this as "other" and didn't mention a language)
Well it's not really a language. It came up in data structures and algorithms course
Yea I think this is what I mean
Accessing an element in an array is always going to be O(1) because it's just a single jump to that memory address from the memory address identifying the start of your array
Great thanks.
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...
that's what I took from it lol
This of course, depends on how the array is implemented. But typically it will be direct memory access and if not it'll be in some sort of linked list kinda thing which will be O(n)
Yeah, but the array primitive will have contiguous memory, otherwise I wouldn't really say it's an array lol - but that's just semantics
Agreed
oooo I see. so basically what they're saying is that after n/2 or m/4 elements are removed, we can finally resize the array down
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
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
Yeah neither do I
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
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
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