- 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
Admin
That's actually the basis for the O(1) quantum sort algorithm. If it turns out that the "multiple parallel universes" theory is correct, then all you have to do to sort in O(1) is randomize with a quantum process, check the data, and if not sorted, destroy the universe.
...
You know, that was much funnier before I thought of it in the context of this blog. I suggest we all make a pact to kill anyone who suggests that this be developed. Would you want to experience the debugging process on that one?
Admin
Nothing like O(rand(n)) efficiency!
Admin
I have never found an appropriate use for a GOTO outside of BASIC when I was 12. Really, there is no excuse for it. If you're using GOTOs, your functions are probably too long. Work on breaking it up into smaller chunks. Oh, and always use the built in sort functions. :-)
Admin
You all complain about O(n). A RSS feed ? OMG ?!! NO !!! 50 items !!! that will take like .... forever !!!!
Admin
The time taken to check if it is sorted would be O(n) :)
Admin
Oops. My bad. You're right. I wasn't a CS major, so I often @#$% up the big-O stuff.
Anyway, I think I can sum up the main WTF with this: [image]
Admin
I worked on a program once where the lead developer coded his own bubble sort routine to sort, at most, 10 items. And on a class that already implemented ISortable.
It then bypassed the bubble sort and took the classes position attribute and put it there in that array position. Needless to say it didn't handle a position of -1 too well.
Same guy also had a profound dislike for collections (coz arrays are faster). The code was littered with redim preserve.....
Admin
I coded an n cubed sort. I forgot what language I did it in. Though whatever language it was, it didn't have a built-in sort. I was in first year university. Fortunately it wasn't needed for a class, so I could sort of forget about it and dump it.
Admin
OK, ignoring the fact that having arrays of strings is a really lousy way of storing data in a language like C# (object with named properties would be so much nicer, no?) ... here's some slightly modified code that does what he did:
List<string[]> arrTotal;
public int RssDateComparer( string[] a, string[] b ) { return DateTime.Parse(a).CompareTo(DateTime.Parse(b)); }
And, here's how to call the sort:
Sooooo much easier. So, yeah, NIH bites.
Admin
Oops, that should be:
return DateTime.Parse(a[2]).CompareTo(DateTime.Parse(b[2]));
Of course, this makes ALL kinds of assumptions about the arrays having enough elements and that the date elements are actually valid and parsable. This is why one should really use objects with typed fields/properties rather than string arrays.
Admin
Read Linus Torwalds rant about GOTOs and remind me never to be hired by you ...
Admin
A 52-card deck on the ninth pass? Liar.
Admin
Misunderstanding of the whole Haiku concept? ...or did you pass a list of random words through the article's sort routine to generate this? In that case, take back my original statement, it's brilliant.
Admin
The official MS.NET way to do this is obnoxious in its own right. If I remember correctly, you have to make an instantiable class that implements an interface, then pass an instance to a Sort method of the collection.
In fact, I think one must make a custom instantiable class just to sort strings in descending order. Pretty obnoxious... it makes this WTF just a tiny bit more forgivable.
Admin
That code looks oddly familiar. I think I wrote something similar a few years ago. Go me.
Admin
GOTO is a tool. Like any tool it works when used properly. GOTOs can mkae code more readable. While any code can be written without GOTOs, sometimes it turns out being more complicated and less maintainable. YES there are times when a large function just can't be broken down, without turning it into a morass of funtion calls.
For SMALL values of N, bubble sort makes perfect sense. (where N is generally less than 20) Bubble sort is EASY to write, and EASY to understand. Sometimes it is just easier to implement a quick bubble sort, than to write a comparator and use a built in sort.
If you had three elements, and ONLY three elements. Lets say they aren't in a list... HOW do you sort them?
Admin
Ok, I admit. I spent far too long working on this last night.
Couldn't manage to do it with JUST min and max though.
(mmm... double-spaced code. Wonder how THAT is happening.)
Admin
Yay for reparsing both dates for every comparison, which is hugely more expensive than the comparison itself.
Admin
No, the official way is to implement IComparable and add a compare function. The way you listed is for cases where you need multiple sorts.
I'm pretty sure the string class implements IComparable.
Admin
Compare every value to every other value even if its already in order, thats not how I do a bubble sort.
Regarding Gotos, how would you get out of a double loop without convoluting your code with an extra procedure, variable or confusing condition.
for(..) { for(..) { ... code ... if (condition) goto exitbothloops
} }
exitbothloops:
...
Admin
Err...worst case for QuickSort is O(n^2). Best case and average case are O(n*log(n)).
captcha: smile...cheese!
Admin
I also like blinking, me.
Admin
That second function deserves a good seeing to as well...
private void Swap(int first) { arrTotal[first][0] %= (arrTotal[first + 1][0] % arrTotal[first][0]); arrTotal[first][1] %= (arrTotal[first + 1][1] % arrTotal[first][1]); arrTotal[first][2] %= (arrTotal[first + 1][2] % arrTotal[first][2]); arrTotal[first][3] %= (arrTotal[first + 1][3] % arrTotal[first][3]); }
Obviously you only ever call this function with the private member. Works fine like that.Admin
I heard they developed a similar routine for the Space Telescope. They called it the Hubble Bubble Sort Algorithm. Only trouble was, they couldn't work out whether to run it or smoke it.
Admin
If a language lacks a labeled break goto is often the clearest way to write some things. Knuth gives a good example somewhere, though I've forgotten exactly where. Goto can also be useful when building certain types of abstractions using a small set of primitives. For instance a lot of the iteration macros (dotimes, dolist, etc...) in Common Lisp are usually implemented in a subset of CL, and those implementations often use gotos.
Admin
In a decent language like PHP, you just need to "break 2;".
Admin
I keep hearing of this stupid bubble sort that everyone hates. I had no clue what the hell it was till I read this article.
Why are people so damn stupid and lazy. Uh, lets not look up on the net the right way to do this. Lets use some F up code to make my life even harder cause I'm a tard.
Admin
In a decent language like php! HAHAHAHAH
try a generic list in C#, so easy, so far less to code, so safe.
php sucks balls and is beyond its time.
Admin
Admin
feizhuliu