- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
TIL Java's
Vector
isn't formally deprecated... Still, TR :WTF: is them not changing it toArrayList
since (as far as I can see) there's no need for synchronization on every single operation. I think that would be a good boost in performance, right?Admin
With the recent influxes of sort-every-time articles, I wonder if there's an
sortedAdd()
function to speed things along? Granted, it would be all :wtf: as anything (i.e. probably assumes the array is already sorted), but it should work just fine, assuming that's the only way you start adding things to the list, right?Filed under: Obfuscating code by hand
Admin
This is pretty special. :wtf:
Admin
I knew I'd seen something like this recently.
Admin
I think there's a SortedSet interface?
Admin
Yep, IIRC
TreeSet
implements it.Admin
http://thedailywtf.com/articles/sorting-cabinets
Admin
This, children, is why you do code reviews.
Admin
huh.... i thought that story looked familliar....
Admin
Also, article reviews, apparently :trollface:
Admin
Oh wow... um.... Sorry, I totally didn't notice when this was farmed out to me. I've informed my editor.
Admin
Maybe we should build an article management app for him/her/it :P
Admin
Apart from the less-than-efficient use of types, this just seems like a simple case of a misplaced sort. Easy to do, it's a simple brainfart, but difficult to track down unless you're looking for it, because the code actually works. If the team is particularly busy and the workload is high, it's a fairly common-or-garden mistake. All in all, looks like a fairly successful deployment, where the team is alert for problems, is evaluating the performance, and have caught and fixed a performance issue in very short order. Job done, good work team.
Admin
Nice umbrella! Is it home made?
Admin
Yes, thank you, I stitched on the sequins myself before I coated it with shit-resistance spray :)
Admin
That sounds like a
glorymarket hole!Admin
I guess you filed the tip off as well so it doesn't get accidentally stuck in the fan?
Admin
Admin
Admin
Isn't the real WTF that nobody looked at the "clever" intern's code before it got deployed to production?
Admin
I think pretty much every first-year programming class on Java or C# is making a List/Vector/whatever class that stays sorted as shit's inserted.
Admin
Hmmm...should have gone with the bubble sort.
Admin
But SortedSet has a different semantic from a sorted List (in particular with regards to preserving duplicate entries)...
Admin
It compiled. It ran. It worked[1]. What more do you need?
[1] Okay, it worked on the test data, which was conveniently a very small set which was already sorted, but why would you want to test with anything more than that?
Admin
Then you want a sorted multiset. A sorted list doesn't make sense because a list is, by definition, sorted by index, not value.
Admin
Let's say you have an array-backed list and you're adding a billion integers to it in reverse sorted order. You could:
Now let's put it in numbers:
30897352853
29897352854986261130
1000000030897352853
So it looks like your plan is slightly more efficient than the one used in the beginning of the article, but many orders of magnitude less efficient than the one at the end.
Admin
Still useful if (any of):
you expect to consult the container significantly more often than you add elements
you use a sorted container that has an interface to add a whole container
you choose not to use an array-backed container but something more like a b-tree.
Admin
It's still O(n + log n) for inserts, whereas a TreeSet is O(log n).
What exactly do you sort a set of sorted sets by?
In which case it's not a list, unless you're making a really weird list implementation that uses binary trees. And you'd still need to update the index of everything after the index you inserted.
Admin
Well, you can sort-of sort the set of sets by the sort of the sets.
[/silly]
Or by the cardinality, or the maximum element, or something like that. Whatever makes sense. (Java lets you specify a custom comparator.)
Admin
That last one should be O(n2 log n), shouldn't it?
Admin
You calculate the position using binary search, but you don't have to recalculate it once you know it. You just have to shift up to n elements one space to the right.
Admin
doo an n logn and an n^2 operation n times.....
i think that would be an n^3 operation wouldn't it?
or am i misreading the algorithm spec?
Admin
Right, so (n * log n * n = n2 log n), right? That is, you put them together as (add * search * insert)?
Admin
You have to do (search+insert) add times.
Admin
Ah, okay.
Admin
http://thedailywtf.com/articles/sorting-cabinets
So no... the code doesn't work. It seems to work, but in all honestly, it doesn't. (Like the aforementioned article submission process)
Admin
Oh, the submission worked fine. The article was well and truly submitted. Yup.
Admin
Admin
The optimum is to do the first option, assuming sorting is inevitable. But I wondered if this is SQL based and, if so, why not have the sort engine return them ordered (think: index).