Sorting, Shuffling, and Searching

Entire books have been written about the art of sorting. More specifically, sorting has been explored in the granddaddy of all Microsoft demo programs, SORTDEMO, which was written for QuickBasic by former Microsoft documentation author Michael Morrow. In a previous life as a sample programmer for the Microsoft languages department, I inherited the Basic version and presided over various enhancements and translations to C, Pascal, and FORTRAN. I later saw translations by others to COBOL (believe it or not) and to C++ with MFC.

If you own older versions of Microsoft C, FORTRAN, Pascal, Basic, or COBOL, you’ve probably seen this program. It randomizes colored bars on the screen and then presents a menu of sorting algorithms: bubble, shell, exchange, heap, and quick. You can see how the different algorithms compare elements and exchange them, and you can compare timings. More annoying, you can hear the difference, because each bar plays a different tone based on its length. I can still hear the difference between a QuickSort and a HeapSort.

Unfortunately, SORTDEMO has never been translated to Visual Basic. I was tempted, but this section is about sorting as a practical task, not sorting as a science. Therefore I’ll present only the sorting algorithm that SORTDEMO shows to be the most efficient—QuickSort. Computer scientists might argue that Quick­Sort isn’t always the fastest. The efficiency of sorting algorithms varies, depending on the number of elements, how random they are, how long it takes to compare elements, how long it takes to swap them, and so on. But QuickSort is more than adequate for the tasks in this book.