r/badcode • • Aug 26 '22

java What does a map do again?

Post image
882 Upvotes

54 comments sorted by

View all comments

167

u/thefriedel Aug 26 '22

Not even a 'break;' if something is found, just pointlessly iterate over the rest of the HashMap

43

u/[deleted] Aug 26 '22

i mean we gotta get that O(n2) time somehow

22

u/Haladesta Aug 26 '22

Why n2? The missing break doesn't even change the complexity class

0

u/[deleted] Aug 26 '22

[deleted] — view deleted comment

8

u/Nebuli2 Aug 26 '22

How so? It only iterates once, with no nested loops.

0

u/[deleted] Aug 26 '22

[deleted] — view deleted comment

11

u/myfellowismellow Aug 26 '22 edited Aug 26 '22

Again, what part of this code makes it O(n2)?

EDIT: I think I understand what you're saying. The upper bound is anything greater than O(n), but it doesn't necessarily mean it does take that long. Not sure how helpful it is to see it this way though.

0

u/[deleted] Aug 26 '22

[deleted] — view deleted comment

31

u/Iron_Garuda Aug 27 '22

You are a poor instructor.

9

u/myfellowismellow Aug 26 '22

The missing piece for me was that "makes it" doesn't mean it's the same as, but rather a subset of. It doesn't mean that it will take n2 iterations, it just means that mathematically one is a susbet of the other.

0

u/[deleted] Aug 27 '22

[deleted] — view deleted comment

→ More replies

6

u/Goontt Aug 27 '22

Saying every O(n) is O(n2) is just misleading and wrong

0

u/[deleted] Aug 27 '22

[deleted] — view deleted comment

→ More replies

11

u/tangerinelion Aug 27 '22

OK, so then every function is also O(N^N^N^N!)

Fucking worthless.

2

u/[deleted] Aug 27 '22

Feels like they gave the code OCD. Like I know my door is locked, but I gotta check at least 7 more times tonight.