Sorting is the process when we place a collection of elements into certain order. Generally speaking, this "order" can be defined personally and also according to that collection. For example, we can have a collection of students' final grades and sort them by decreasing order. Also, searching engine can sort results of your search by different "rules", like most popular, most relevant, most recent, etc.
However there are various means and kinds of sorting, this week we started with the basic one, which is just sorting a range of numbers into increasing order. Sorting other collections like strings are similar to sorting numbers, where same insights can be applied. Therefore, in following discussion we only focus on sorting numbers.
Basic steps about sorting numbers:
Sorting numbers depends on two simple steps: First we need to compare number A to number B, to find which one is smaller. Second, we need to change the relevant position of A and B.
These two steps seem really easy, but each step can be applied by different methods. Therefore, there are many ways to sort numbers and this through us the question how can we select the "most optimal" method, which means the smallest run time and complexity.
Quick sort:
Quicksort works by partitioning the list into two halves: those items less than the pivot item, and those greater than or equal to the pivot item. For a list [5, 1, 7, 3, 9, 12] and choosing pivot 5, we can first sort it into [3, 1, 5, 7, 9, 12]. After that, we recursively quick sort the left half and right half until it cannot be separated. The run time depends on two steps. First, how many times can the list of length n be divided? The answer should be log2(n) and the runtime is in O(ln n). Second, in final state, how many items shall we compare? The answer if a linear function of n. Therefore the runtime is in O(n). then the combination of these two steps are O(n ln n).
Note that there exist a worst case of this algorithm. The problem occurs when careless choosing the pivot. If each time the pivot we choose happen to be smallest or largest of the array, all the rest numbers will be put into one half and leave the other half empty. When this case happens, the runtime grows to O(n^2) because we indeed divide the list n times and in the final state we compare n times.
Selection sort:
Selection sort works by repeatedly selecting the smallest remaining item and putting it where it belongs. This is rather simple if we want to compute the runtime. First it need to search the smallest number by looping. Then each minimum selection counts O(n) times. Secondly, the rule "putting it where it belongs" means we need to find a position such that A[n - 1] < number we selected < A[n]. This means we also need to search be looping, which creat another O(n). In all, that is O(n) * O(n) = O(n^2).
Although there are also worst case and best case for selection sort, the runtime is still of O(n^2), which means it is a rather stable algorithm.
Insertion sort:
Note that this algorithm has a best case. When it comes to a already sorted list, it only need O(n) comparison but just O(1) swap! Then the total runtime shrinks to O(n).
Bubble sort:
However, this algorithm has a significant benefit if we are not going to sort a list but detect whether is is already sorted. In other words, it is well performing on sorted list, only O(n).
Merge sort:
Merge sort splits the list in half, sorts the two halves, and then merges the two sorted halves. When it comes to "split into halves", you can imagine it has similar performance with quick sort which also half the list. Actually, the only difference between these two algorithm is that Merge sort implement "sort" in merge steps and quick sort implement it in dividing step. The rest of them are just the same.
Merge sort:
Merge sort splits the list in half, sorts the two halves, and then merges the two sorted halves. When it comes to "split into halves", you can imagine it has similar performance with quick sort which also half the list. Actually, the only difference between these two algorithm is that Merge sort implement "sort" in merge steps and quick sort implement it in dividing step. The rest of them are just the same.
Therefore the runtime is also O(n ln n).
Conclusion:
These are almost the most common sorting algorithm. However there are always new sorting method coming out and perform well in different cases. Sorting is a big problem in computer science and still need hard work to accomplish better performance. The above discussion is not all about sorting and you may explore more if you want.