qsort :: Ord a => [a] -> [a] qsort [] = [] qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater where lesser = 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-wor…
Quicksort is the new Hello World
31–40 of 64 posts
Re: Quicksort is the new Hello World
#32Hello 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_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 resultRe: Quicksort is the new Hello World
#33Re: Quicksort is the new Hello World
#34Re: Quicksort is the new Hello World
#35Quicksort 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.
Re: Quicksort is the new Hello World
#36Re: Quicksort is the new Hello World
#37Gosu's example code does a good job at covering a lot of aspects in ~50 lines of code:
Re: Quicksort is the new Hello World
#38And btw, here's the answer in F#:
let rec qsort = function
| [] -> []
| x::xs -> let smaller,larger = List.partition (fun y -> yRe: Quicksort is the new Hello World
#39Print 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]
Re: Quicksort is the new Hello World
#40I 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.
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.