5 ms·
Quicksort is the new Hello World
- gsivil 16y agoI think your point is valid and of course interesting for programmers, but KR's "Hello, world!" is still relevant for students and novices. Maybe reading/writing on a file would be more useful nowadays, since it does not demand knowledge of algorithms.
- ollysb 16y agoA large part of the value of a hello world is to check that you've actually installed the language correctly and can compile/run code. Printing hello world is just the simplest way of proving it.
- ctdonath 16y agoIt is very relevant for total novices. When someone has no concept about programming, this is the fastest way to "here are the barest basics, and you have already written a program." Quick sort means nothing to someone who does not yet grok compiling and output, much less sorting as an algorithmic process. Question isn't whether "hello world" is obsolete, it's the level of knowledge of the author. A rank noob needs to see the simplest possible program, not the highest density functionality.
- dshankar 16y agoFor a veteran programmer learning a new language like Haskell, I agree completely. For a novice programmer learning their first language, they probably don't understand the quicksort algo and probably have no concept of arrays or any data structure. Visually displaying "Hello World" is very simple and teaches you two basic parts of a language: - Syntax - Printing stuff On another note, someone learning HTML for the first time will have no reason to do quicksort.
- fleitz 16y agoI don't believe HTML is Turing complete, and thus it's difficult to argue that it is indeed a language for computing.
- T-R 16y agoI don't think "Hello World" will ever cease to be useful for the simple task of making sure everything's installed and operating correctly. Quicksort is certainly beautifully concise in Haskell, but it's not in all languages, and I want a low probability of errors when I'm just getting things up and running for the first time.
- dzorz 16y agoAlthough this version is very simple, concatenating and creating lists at runtime ruins performance (and it takes extra memory). Please make it a good example by doing a version of Quicksort with in-place partition.
- Nitramp 16y agoThat's actually a nice nab for functional programmers. They run around all the time telling how nice their language of choice is, showing off quicksort in three lines of code. But please don't have the audacity of pointing out that original quicksort was supposed to be an efficient in place sort, not a mental exercise in beauty of a language. Then you get a Haskell version that is in place, and supposedly fast. It's about 10 lines longer than the C version, uses Monads, ST, recursion, "unsafeRead/Write", "rscan", "lscan", "swap", "sloop", etc., all together about 20 concepts, built-in functions, etc. Also, of course, if it really should be fast, you have to use several compiler hints and macros to make it type specialize and all. Obviously. The C version is shorter, uses nothing but array accesses, do/while, if, int comparisons, and recursion. If you want to explain the C version to a novice programmer, it will maybe take you half a day. The inplace Haskell version? Uhhh, yeah. I can't help it, but things like these leave me with the impression of Haskell being a theoretically really nice language whose utility in actually getting things done seems somewhat questionable.
- bmcleod 16y agoHello world is valid for checking that you have everything installed properly and it's the fastest way to make sure you get the most basic syntax of the program. It's not like the first thing you're going to do is write out all of quicksort to see if it's easy. You're going to fiddle with numbers an arrays a bit first, particularly if your language has some kind of interactive console. In fact, I wouldn't be surprised if the first thing you do in the language is actually 1+2. There's quite a bit of syntax you need to learn before quicksort becomes a relevant test. If you're looking for a good language to build a quick website backend in you probably don't even care about that, you'll care more about having nice ways of passing information around and checking that it has a decent inbuilt sort method.
- sid0 16y agoI think something simpler would be a better Hello World equivalent. The first program I generally write in any functional language (usually as a sanity check) is one to calculate the factorial of a number.
- l0stman 16y agoqsort :: Ord a => [a] -> [a] qsort [] = [] qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater where lesser = filter (< p) xs greater = filter (>= p) xs I know nothing about Haskell but I don't think this code implements the original quicksort algorithm which sorts the input in-place. Moreover the two-pass of filter over the list and the concatenations cause unnecessary overhead. Thus, even if the sample code is simple and elegant, a real-world implementation would surely be a bit longer than the given example.
- amalcon 16y agoIt would be pretty difficult to write an in-place sort in Haskell, given that it only has mutable state through monads (and I doubt anyone wants to go into a monad just to sort something). GHC is pretty good at optimizing list concatenations, so that's probably not a big deal either. You're absolutely right about the two-pass filter, though.
- sizzla 16y agoIt's not that good, which is why they gave up on using quicksort in Data.List .
- pmjordan 16y agoIn fairness, in-place sorting usually isn't what you want in functional programming. Immutable data structures help enforce the pure functional constraints. Of course, in practice, your library code will probably make a mutable copy, sort that in-place and then return an immutable copy/representation of the result, which is hopefully more efficient. (Disclaimer: I'm no Haskell programmer; my FP experience is limited to Lisp dialects) EDIT: As a curious example, Clojure's sort function actually converts the sequence to a (Java) Array and calls java.util.Arrays.sort() on it, then turns it back into a sequence: https://github.com/clojure/clojure/blob/b578c69d7480f621841ebcafdfa98e33fcb765f6/src/clj/clojure/core.clj#L2329 https://github.com/clojure/clojure/blob/b578c69d7480f621841e...
- sizzla 16y ago
- sb 16y agotl; dr, but a quick note: the presented algorithm takes the first element as the pivot element p: qsort (p:xs) = ... This is not recommended as it results in worst-case behavior on sorted input lists. (Another commentor correctly pointed out that it requires extra memory for the intermediate lists, too.)
- Deestan 16y agoIt is just a toy implementation. If we're going to worry about practicals, then it's not recommended to use your own general purpose sorting implementations at all. For general purpose sorting, the standard library "always" has the best implementation.
- sizzla 16y agoAny knowledge about your data set, how much of it varies, etc can vastly improve your performance over any canned standard library solution. Sorting ints? Radix-sort that mofo.
- dagw 16y agoExcept it is also the canonical look-how-awesome-Haskell-is example that keeps getting trotted out all the time. Why aren't there more canonical examples of Haskell being both simple and awesome as well as fast and correct?
- alvindos 16y ago[url=http://www.ucvhost.com]cheap http://www.ucvhost.com]cheap vps[/url]
- eliben 16y agoSadly, many programmers don't understand how quicksort works. This makes it somewhat unsuitable to be a hello-world.
- yoyar 16y agoHello World is there to validate that you can setup, compile and run a program in your new language of choice. Complicating that process by trying to write something non-trivial defeats the purpose of the exercise. I call BS on this article.
- mwill 16y agoI still write and run Hello World in languages I am well versed in when I start working in a new environment, purely as a sanity check. In many cases it is the simplest starting point to ensure you have a working environment. As far as familiarizing myself with a new language, I usually write a Hello World, then add some variables, and some functions or whatever is relevant, and just focus on the syntax of the language, or in the case of a platform or framework, take a look at building trivial components. Not a language, but a platform example: I recently explored ExpressJS (If you're not familiar, its a framework for node.js web apps.) and my workflow was: Make a hello world server, Add a few routes and a static handler, Add some views, Add some middleware, Build it into a basic blog. (Because it's always a blog first, right?) The whole process was quite quick and gave me a very strong jumping point for ExpressJS. Implementing quicksort or something similar as my first crack would probably have been pretty useless. So for me its all about context; I tend to start at Hello World and quickly grow something simple, but relevant to the language or platform.
- sgt 16y agoI'm proficient in Hello World in 24 languages. And now you say my knowledge is obsolete?
- quadhome 16y ago"For every complex problem, there is an answer that is clear, simple-- and wrong."
- sizzla 16y agoBeautiful. We really need more cynicism on HN.
- noamsml 16y agoI think I'm not alone when saying that the first thing I write in a new language I learn is some sort of project that requires a good understanding of the language's function-call model, of its object model, of a few specifics (strings, regices, data structures, arithmetic [depending on what I find interesting]), and that requires me to use some part of the standard library not readily available or to decompose my code into multiple files (or both). This helps me understand better what I'm doing, what I like about the language and what I don't. Examples of such projects include IRC bots, Lisp Interpreters, twitter clients, etc. (The one exception is Haskell, where I haven't gotten far enough to understand Monads well so I just wrote a completely functional RPN calculator).
- alextgordon 16y agoThis article is misguided. Hello World serves two (and only two!) purposes: 1. It's traditional 2. It lets you check everything is working It isn't a way to evaluate or compare languages. How could it be? It only uses a very small fraction of the language. Note that the Hello World for C is rather long and inelegant. But C itself is definitely an elegant language. So its predictive power is poor. QuickSort is neither traditional nor something a beginning programmer could use to check everything is working. Therefore it is not a substitute for Hello World. - - - If you want to get a feel for a language with one code snippet, allow me to introduce the Trabb-Pardo Knuth algorithm: In their 1977 work "The Early Development of Programming Languages", Trabb Pardo and Knuth introduced a trivial program which involved arrays, indexing, mathematical functions, subroutines, I/O, conditionals and iteration. They then wrote implementations of the algorithm in several early programming languages to show how such concepts were expressed. http://en.wikipedia.org/wiki/Trabb_Pardo%E2%80%93Knuth_algorithm http://en.wikipedia.org/wiki/Trabb_Pardo%E2%80%93Knuth_algor... ask for 11 numbers to be read into a sequence S reverse sequence S for each item in sequence S do an operation if result overflows alert user else print result
- nkassis 16y agoAfter I write my "Hello World!" program I usually graduate to writing a tictactoe game. Tictactoe has enough stuff to learn the basics of most languages.
- LostInTheWoods2 16y agoI usually implement a tic-tac-toe game as my first project when learning a new language. Tic-tac-toe, properly implemented, will give you a pretty good tour of a language. But with that said, Hello world, still serves it purpose.
- sizzla 16y agoThis is a great example of making something that is challenging for a novice, yet not so challenging they give up. The reward is much higher than watching some numbers show up in the correct order and you tend to cover more of the language (2D array or your own abstraction of such, I/O, control flow, etc.).
- _tef 16y agoIt is like fizzbuzz/binary search in that most people think they can write it and they get it wrong. Quicksort requires careful selection of pivots, and is unstable. (Many scripting languages (perl, python) are opting for stable sorts) Check out Bentley&McIllroy's Engineering Quick Sort for a guide about the problems implementing a production ready quicksort. If you're going to teach them something easy, simple and relatively hard to implement badly, teach them merge sort. Then teach them adaptive merge sort.
- sizzla 16y agoI would rather teach them: 1) how to write a program that guesses a number you thought of by doing binary search. 2) solve 2-queens 3) solve n-queens 4) solve knapsacking or some other combinatorial problem requiring memoization or DP. Of course, I wouldn't tell them any of those buzzwords that are meant to scare them away from just hacking on it. Their mind will be blown regardless of the language they used and they'll get a feel for O() without even opening Cormen.
- arvinjoar 16y agoSomeone already pointed out that you try out "Hello World" to see if everything is working correctly. Anyway, the author fails to make the distinction between is and ought to be, it's sloppy.
- billmcneale 16y agoQuickSort is still a very small problem that is not going to show much about the language. Gosu's example code does a good job at covering a lot of aspects in ~50 lines of code: http://gosu-lang.org/intro.shtml http://gosu-lang.org/intro.shtml
- DanielBMarkham 16y agoYet another article that completely misses the point. The point of "Hello World" is to show a noob how to fire up the editor, compile, and see something happen. And btw, here's the answer in F#: let rec qsort = function | [] -> [] | x::xs -> let smaller,larger = List.partition (fun y -> y<=x) xs qsort smaller @ [x] @ qsort larger
- sizzla 16y agoThis looks like yet another Haskell "variant." From what I can tell this sort does not occur in place and thus would not make a particularly good quicksort. Memory use, number of accesses per element, etc. This style of programming is more suitable for heapsort or maybe mergesort.
- zbanks 16y agoYou can't exactly sort "in place" in functional languages like F#/Haskell... at least trivially.
- fleitz 16y agoGiven the level of abstraction in most modern programing languages/ operating systems it's really hard to say that any modern language could implement quicksort in the most strict definitions. For example, is a quicksort 'in-place' if your memory gets swapped to disk?
- pauljonas 16y agoThe only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages: Print the words hello, world This is a bug hurdle; to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy. The C Programming Language [Kernighan, Ritchie]
- seanmcq 16y agoThis test gives an extreme advantage to lazy functional languages that they don't enjoy in any realistic context. Trying to come up with a single test that is good turns out to be surprisingly hard. In modern languages I'm looking for the ability to do clean functional code, stateful object code, async programming, a solid module system, and a type system that doesn't get in my way. I really can't imagine a single 5 line program that would encapsulate all of these areas.
- yoshiks 16y agoDo we all agree to teach from quicksort, to complete newbies of programming, while most experienced people had hard time to understand? Or simply just read this thread and imagine if you are just 12.