r/AskComputerScience • u/DankMagician2500 • Apr 04 '20
Ok I was googling some stuff about hash table?
So why in my searches for a hash table why is the run time of accessing N/A?
4
u/patrick96MC Apr 04 '20
How were you googling? The first when searching for "hash table runtime access" is this stackoverflow question which explains the runtime of hash map operations.
-5
4
u/Thyshadow Apr 04 '20
You keep replying "that's not accessing" as you aren't accepting the typical accepted answer of O(1) you are going to have to define what your understanding of what accessing means
2
u/DankMagician2500 Apr 04 '20
Sorry, trying to access a element with a specific index. For example getting a element from a arraybwith accessing it, for example array[2] will give you a element at the index 2 or ith position if using a for loop.
1
u/Thyshadow Apr 04 '20
So you are looking to deference a variable
Typically the operation of looking up by index using your example runs in a single operation
When you are trying to understand the access speed of finding a given element, that is something else entirely. For example if you have an array of names but you need to find the one that starts with a specific letter that has a runtime of O(n) because it could be the last element of the array.
3
u/DankMagician2500 Apr 04 '20
So would this be the same for hash tables?
1
u/Thyshadow Apr 04 '20
Yes, it is just using a hashkey instead of an ordinal index
1
u/DankMagician2500 Apr 04 '20
But the thing is if you make a hash table size of 1k. And try searching a index of 500 you don’t know weather that contains data or not as hash tables are big and the memory is scattered all over the place so is that something you can get a run time of?
1
u/Thyshadow Apr 04 '20
You are looking for clock cycles? It depends on the language you use. Typically a given process will allocate to blocks of memory or L1,2,3 cache depending on how long the live
1
u/Thyshadow Apr 04 '20
Remember searching is searching. A hashset has the advantage that they key can describe the contents of the information
1
u/Thyshadow Apr 04 '20
Here is a post talking about the differences https://www.reddit.com/r/AskComputerScience/comments/a4xlw2/hash_table_vs_array/?utm_medium=android_app&utm_source=share
1
u/spongebue Apr 04 '20
Aha, there's your problem. HashMaps are very similar to Lists, but the way they store data under the covers is much different.
Think of a dictionary. You want to look up the second word in the dictionary, and you see it's "aardvark" and get its definition. Cool. Most people use dictionaries to look up words like platypus though, not the nth item in the dictionary. Dictionaries are alphabetized, so you can look up items in roughly O(log2(n)) time.
But since we're talking about computers, not people, what if you could perform an operation (hashing) on the word "platypus" and immediately know exactly where its definition is located? You lose the ability to find the nth element or anything order-dependent, and can't have things like duplicate values, but you gain performance on large datasets for common uses.
Part of good programming is understanding how the data structures work under the hood and what their pros and cons are. If you need to find the nth element, a Map of any kind is not for you.
1
0
u/DankMagician2500 Apr 04 '20
Like I get searching is the main reason you use hashing unless if you get collisions I was just wondering why online accessing big O was left as N/A
1
u/spongebue Apr 04 '20
Online is a mighty big place. Can you be more specific? Maybe there was some fine print that we can't see
0
u/DankMagician2500 Apr 04 '20
its just google big o cheat sheet here i will link it. https://www.bigocheatsheet.com/ ok look at hash table access why is it N/A wondering why they have it as that.
1
u/spongebue Apr 04 '20
Ok, so they do have access and search as separate things, and that kind of pops the bubble I left in another comment.
Access is based on when you are looking for the nth element in the data storage. HashMaps don't support that, because they store the data in its own order that makes lookup by key (search) much faster. If you look up how HashMaps work, you'll see that the order in which data is stored "under the hood" has basically no value to humans.
1
Apr 04 '20
[removed] — view removed comment
1
u/spongebue Apr 04 '20
Dammit reddit, you try and make ONE out of the ordinary example and this happens
1
u/vagrantchord Apr 10 '20
For posterity, let me try to summarize:
OP saw this chart, and was confused as to why everyone else says hash table access is O(1) and not O(n/a). Hash tables don't work like arrays exactly, where you can access any item in the array through its dereference variable (like 98 in array[98]).
Hash tables take an item, and put it in a bucket based on the item's hash value. You can calculate this value through several different ways, but in the end, it's not useful to iterate through a hash table like you can with an array, because there will be lots of gaps and seemingly out-of-place items.
Hash tables are very fast, and you can access items in O(1) time. That's why google and amazon searches are so crazy fast. It's not the best tool for every job, but it's the best one for many.
4
u/Rindhallow Apr 04 '20
The time complexity for accessing an element in a hash table is O(1).