The tiniest C sort function?
cs.dartmouth.edu
The tiniest C sort function?
1–10 of 16 posts
Re: The tiniest C sort function?
#2Re: The tiniest C sort function?
#3If 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?
#4Re: The tiniest C sort function?
#5> 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?
#6The tiniest sort in most other languages: "sort". And it probably runs in not-O(n 2) time...
Re: The tiniest C sort function?
#7> 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 :)
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?
#8But it's still a selection sort.
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?
#9C 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?
#10The tiniest sort in most other languages: "sort". And it probably runs in not-O(n 2) time...
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 .
I'm sure qsort() is older than even 3BSD, but that's the oldest source base I have sitting on this box.