For illus­tra­tion pur­poses 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 generic to avoid com­pli­ca­tions and per­for­mance over­heads asso­ci­ated 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 function:

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 generic ver­sion of my Quick­Sort imple­men­ta­tion would do in com­par­i­son, here’s the results:

image

image

Generic equal­ity or com­par­i­son is pretty slow, so where pos­si­ble (not in this case unfor­tu­nately) you should mark func­tions that requires generic com­par­i­son ‘inline’ to boost per­for­mance.

Share
  • Anony­mous

    This is not quick­sort, the con­cat is O(n)