Many older languages provide an array structure, but not any direct way to sort the array. This leaves the programmer to write her own sort methods to suit the situation. Visual Basic provides a sort method that works well in most circumstances. The speed of sorting data is key, so understanding the notation that describe the algorithm's speed is critical.
Big O Notation
Before discussing the advantages and disadvantages of Visual Basic's sort method, it helps to have a quick crash course on "Big O" notation. This scheme provides computer scientist with a fast way of describing how quickly an algorithm works on large sets of data. The possible Big O values for an algorithm run, from fastest to slowest:
O(1) < O(log N) < O(N) < O(N log N) < O(N2)
If an algorithm runs in "O(1)," then it will take the same amount of time, no matter how much data is in the set. If it runs in "O(N)" time, then the time will increase at the same rate that the amount of data increase. If it runs in "O(N2)" time, then the time will increase dramatically with each added piece of data.
About the Sort Method
The Visual Basic sort method uses the Quicksort algorithm. On average, the Quicksort can run in O(N log N) time. Even though this is toward the slower side of the Big O values, sorting is a relatively time-consuming operation, and O(N log N) is fast for a sorting algorithm. Most sorting algorithms run in O(N2).
Even the Quicksort isn't perfect: data that is sorted in exact reverse order will still require O(N2) with the Quicksort.
Advantages
The biggest advantage to using Visual Basic's sort method is that it is mature code using a well-known sorting algorithm. Microsoft has already written and tested the code, so all that remains for the programmer is to call upon it. They also choose the Quicksort algorithm which, under normal circumstances, is among the fastest sorting algorithms for generic data.
Unsuitable Situations
The default Sort method has a few disadvantages. The biggest is that, while it does better then other sorting algorithms with unsorted data, if the programmer knows in advance that the data set will be almost perfectly sorted, then he can normally run the Selection Sort more quickly. The Selection Sort averages O(N2) for unsorted data, which is much slower than Quicksort, but run in O(N) for data which is already or very nearly already sorted, which is much faster than Quicksort. In addition, if the data to be sorted is read from a data source where it takes dramatically longer to write data than to read it (such as a flash USB drive), selection sort is faster.