F# – Simple QuickSort implementation

For illus­tra­tion pur­pos­es only, here’s how you can imple­ment the Quick­Sort algo­rithm in a few lines of code:

image

Note that I’ve not made the list gener­ic to avoid com­pli­ca­tions and per­for­mance over­heads asso­ci­at­ed with deal­ing with gener­ics. Even then, my 5-line imple­men­ta­tion pales in com­par­i­son with the built-in sort func­tion:

image

This just goes to show how good the built-in func­tions are Winking smile!

In case you’re won­der­ing how a gener­ic ver­sion of my Quick­Sort imple­men­ta­tion would do in com­par­i­son, here’s the results:

image

image

Gener­ic equal­i­ty or com­par­i­son is pret­ty slow, so where pos­si­ble (not in this case unfor­tu­nate­ly) you should mark func­tions that requires gener­ic com­par­i­son ‘inline’ to boost per­for­mance.