TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The tiniest C sort function?

19 pointsby amjithover 15 years ago

6 comments

jacquesmover 15 years ago
If you program in this style you are simply asking for trouble.<p>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.<p>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.<p>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.<p>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.<p>Of course it is a fun little exercise but it really is an example of exactly how <i>not</i> to program in C.<p>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.
评论 #1048462 未加载
petewardenover 15 years ago
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...
city41over 15 years ago
&#62; Use obsolescent (pre-void) syntax. (68 bytes)<p>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 :)
评论 #1048325 未加载
mrcharlesover 15 years ago
But it's still a selection sort.
评论 #1048384 未加载
jrockwayover 15 years ago
The tiniest sort in most other languages: "sort". And it probably runs in not-O(n<i></i>2) time...
评论 #1048335 未加载
lacrossegmover 15 years ago
template&#60;typename Iterator&#62; void QuickSort(Iterator first, Iterator last) { std::sort(first, last); } // C++ &#62; C, need to #include &#60;algorithm&#62; and &#60;iterator&#62;