There are various kinds of sorting methods. We will discuss the most common ones in the following text. At first, we need to know what Big O notation is. Big O notation characterizes the function according to its growth rate: different functions with the same growth rate may be represented using the same O notation. Big O notation usually provides an upper bound on the growth rate of the function.The other types of growth are described by using the symbols o, Ω, ω, and Θ.
1) Insertion sort builds the final sorted list one item at a time, however, this method is less efficient on large size lists.The worst case is O(n^2), the best is O(n), and the average is O(n^2). Now let's sort the sequence {3, 7, 4, 9, 5, 2, 6, 1} from small to big by using insertion sort. We will do the following step:
3 7 4 9 5 2 6 1
3 7 4 9 5 2 6 1
3 7 4 9 5 2 6 1
3 4 7 9 5 2 6 1
3 4 7 9 5 2 6 1
3 4 5 7 9 2 6 1
2 3 4 5 7 9 6 1
2 3 4 5 6 7 9 1
1 2 3 4 5 6 7 9
2)Selection sort is also inefficient on large lists. The algorithm of selection sort is that it divides the input list into two parts: the sublist of items already sorted, and the sublist of items remaining to be dealt(rest). It proceeds by finding the smallest(or largest) item in the unsorted sublist, exchanging it with the leftmost unsorted element, and move one element to the right. The best, worst, and average cases are the same that is O(n^2). The following image is an example.
3) Bubble sort works by repeatedly stepping through the list, it compares each pair of adjacent elements and swaps them if they are in the wrong order, and it is also not quite efficient on larger size lists. The worst and best cases are O(n^2), and the average is O(n).The following image is an example of bubble sort, and we can notice that it takes lots of time to run.
4) Merge sort divides the unsorted list into n sublists, each containing 1 element, and it repeatedly erge sublists to produce new sorted sublists until there is only 1 sublist remaining. The worst, best , and average cases are the same that is O(n log n).Here comes the example.
5) Quick sort at first picks an element as a pivot from the list, and then reorders the list so that the values less than the pivot come before the pivot, and the greater ones comes after it. At last, it will recursively apply the above steps that is choosing the pivot and comparing the value to put it in the correct order. Unlike the merge sort, the worst case of quick sort is O(n^2). Here is the example.
By learning the sorting and efficiency part of computer science, I can know what computer scientists think about the algorithms, and how they improve the efficiency, and this can extend my ways of thinking about a problem. Also, saving time is the best.