Live data from Hacker News

The tiniest C sort function?

cs.dartmouth.edu

11–16 of 16 posts

Re: The tiniest C sort function?

#11
post #8

But it's still a selection sort.

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 optimizati…

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?

#12
post #9

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 c…

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?

#13
post #12
post #9

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 c…

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.

> no one actually does that for anything that needs to be maintained

You wish.

Re: The tiniest C sort function?

#14
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?

#15
post #7

Earlier quoted context omitted.

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.

Ah, I guess it's just main that implicitly returns 0.

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.
Post reply on HN