News for hackers. Stuff that matters.
Re:The tiniest C sort function? by jacquesm on Tuesday January 12, 2010 @03:10PM (#1048386)
If you program in this style you are simply asking for trouble.

C is a great little language but I've seen it be described as a racecar. Take too many turns on two wheels and you'll end up regretting it.

There is absolutely no benefit from trying to cram something like this in to two lines other than to show off your C fu, it sets a bad example and and makes for very obscure bugs.

A defensive programming style with clarity in mind rather than weird little optimizations like this are a great way to get good mileage out of what is essentially a luxury assembler.

The lower the level of the language you are working the more important clarity is. As soon as you start to rely on obscure language features or possibly even implementation dependent tricks you clearly no longer have the mindset required for long term maintainable code.

Of course it is a fun little exercise but it really is an example of exactly how not to program in C.

Incidentally, this code will only compile on C99 standard compilers and even then it will probably give you a warning or two about missing return types.

Re:The tiniest C sort function? by ajross on Tuesday January 12, 2010 @03:38PM (#1048462)
Must you be so humorless? It's a clever hack, designed to show how little syntax is required to capture a given algorithm. The admonishment against programming style seems silly: no one actually does that for anything that needs to be maintained (at least no one who can write a correct selection sort). Lighten up, basically.
Re:The tiniest C sort function? by jacquesm on Tuesday January 12, 2010 @03:43PM (#1048477)
> no one actually does that for anything that needs to be maintained

You wish.

Re:The tiniest C sort function? by petewarden on Tuesday January 12, 2010 @04:19PM (#1048568)
I'm tempted to waste an afternoon to see if I can make a smaller implementation using bogosort. Shuffle the input randomly, check if it's sorted, if not, shuffle again. Hey, if we're allowed to use selection sort...
Re:The tiniest C sort function? by city41 on Tuesday January 12, 2010 @02:13PM (#1048234)
> Use obsolescent (pre-void) syntax. (68 bytes)

If memory serves, that means the method now implicitly returns int and has an implicit "return 0;" at the end. Sure the source code is smaller, but the actual machine code is larger. Going for the shortest source code is pretty silly, but then this isn't exactly a serious endeavor :)

Re:The tiniest C sort function? by tedunangst on Tuesday January 12, 2010 @02:49PM (#1048325)
Implicitly returning int is not the same as implicitly returning 0. If it falls off the bottom, the return value is undefined. No additional machine code.
Re:The tiniest C sort function? by city41 on Tuesday January 12, 2010 @02:58PM (#1048349)
Ah, I guess it's just main that implicitly returns 0.
Re:The tiniest C sort function? by azim on Tuesday January 12, 2010 @05:02PM (#1048685)
If memory serves correctly, I believe the typical calling convention for x86 is returning ints in %eax. So the caller will interpret whatever happens to be in %eax at the time as the return value.
Re:The tiniest C sort function? by mrcharles on Tuesday January 12, 2010 @02:08PM (#1048225)
But it's still a selection sort.
Re:The tiniest C sort function? by ajross on Tuesday January 12, 2010 @03:09PM (#1048384)
And thus one that works fine for many uses. Most "sorting" happens (at least when measuring "most" by line of code written) because you're about to display something to the user. Using a quadratic algorithm on the few hundred entries a person can actually interpret is hardly a problem. Pick the tool for the job (though, as pointed out elsewhere, that tool should probably be a library function). Algorithmic optimization isn't always as important as your textbooks tell you it is.

If I were going to nitpick about this kind of exercise, I'd pick the insanity of compressing the whitespace (and symbol naming) of the thing, which a trivial deterministic tool can do. It represents no insight on the part of the author.

Re:The tiniest C sort function? by icefox on Tuesday January 12, 2010 @03:38PM (#1048461)
Well sure, until you hit the case where two years down the road that 100 items is now 1,00,000 and if you had only used the built in function that does qsort...

I would always choose a sorting function from a library over some hand grown thing (with no tests no doubt). They are too easy to make mistakes. Sorting functions are one of those things you get for free in [nearly] every language, writing a new one is usually just a waste of time.

Re:The tiniest C sort function? by jrockway on Tuesday January 12, 2010 @02:38PM (#1048293)
The tiniest sort in most other languages: "sort". And it probably runs in not-O(n2) time...
Re:The tiniest C sort function? by scott_s on Tuesday January 12, 2010 @02:53PM (#1048335)
C99 has a sort function that is a part of the standard library. Clearly, the point of the exercise is not calling a library function, but how most compactly one can express a sort algorithm.
Re:The tiniest C sort function? by ajross on Tuesday January 12, 2010 @03:14PM (#1048391)
Hey, cool. ANSI C89 has a sort function that is part of the standard library too! So does 3BSD! Small world. :)

I'm sure qsort() is older than even 3BSD, but that's the oldest source base I have sitting on this box.

Re:The tiniest C sort function? by lacrossegm on Tuesday January 12, 2010 @05:25PM (#1048729)
template<typename Iterator> void QuickSort(Iterator first, Iterator last) { std::sort(first, last); } // C++ > C, need to #include <algorithm> and <iterator>