5 ms·
Shortest Quicksort Ever?
- ghshephard 15y agoUsually, when I see an opaque post like this, I'll not worry too much because I figure it's really only relevant to the (very, very, very small) minority of the people who understand it. But it's only 61 characters, and I'm certain that I should be able to walk my understanding of quicksort (refreshed with http://en.wikipedia.org/wiki/Quicksort http://en.wikipedia.org/wiki/Quicksort) - but I'm stumped. And so, I beging to lose another two hours of my life as I attempt to understand what this interesting syntax mechanism for representing algorithms consists of...
- scarmig 15y agoThis is a good opportunity to show off the (fake) Haskell quicksort: qsort :: (Ord a Bool) => [a] -> [a] qsort [] = [] qsort (x:xs) = qsort (filter (<= x) xs) ++ [x] ++ qsort (filter (> x) xs) Not 61 characters, but it has the added benefit of being comprehensible!
- chamakits 15y agoSaw your code, couldn't believe how awesome it was, and made an effort to make it shorter just for kicks. (Warning though, I'm pretty new to Haskell.) Did a bit of "cheating" here and there, since the purpose is for it to be as concise as possible, and got it to 50 characters. I'm sure someone can get it lower :P f=filter q[]=[] q(h:t)= q(f(<=h)t)++[h]++q(f(>h)t
- 1331 15y agoI got it down to 47 characters and improved the performance by moving the less common case to the bottom. (Not that is matters since this implementation sacrifices performance for elegance, but playing code golf with Haskell destroys the elegance anyway...) f=filter q(h:t)=q(f(<=h)t)++h:q(f(>h)t) q _=[]
- chamakits 15y agototally stole your idea and found a way to do it in 46 characters :P f=filter q(h:t)= q(f(<=h)t)++h:q(f(>h)t) q a=a
- burke 15y agoI found this J example on StackOverflow. 43 characters, excluding name, assignment operator, and optional whitespace. quicksort =: (($:@(<#[) , (=#[) , $:@(>#[)) ({~ ?@#)) ^: (1<#) http://stackoverflow.com/questions/2603739/what-is-the-most-concise-programming-language http://stackoverflow.com/questions/2603739/what-is-the-most-...
- chamakits 15y agoO.O .....Oh wow...I truly have NO idea how to read this >.<