Sorting and Efficiency
Last week, we covered several sorting method and their running time(in bigO notation). In 108, we learnt three kinds of sorting algorithms. They are bubble sort, insertion sort and selection sort. All three cases take n^2 time in the worst case. Selection sort need to go over the list in every outer while loop, which makes it have the same running time regardless of best or worst case. Insertion sort has a linear running time when sorting a sorted list since the insertion part need to compare the element with the elements before this index until it finds the right position. We did not cover Bubble Sort in 148, so I just ignore this algorithm.
In 148, we have some "smart" sorting algorithms. The first one I want to talk about is mergesort. We proved the correctness and running time of mergesort in CSC240. By strong induction, we have the algorithm that returns the sorted list for each half of the list and then merge it, which should return a sorted list. And the running time mostly depends on the recursive calls not the merge function, which results in the nlogn running time for best or worst case.
Another sort we are required to know is quick sort, which is quite different from the merge sort but both of them use recursive method. The basic idea of quick sort is to choose a pivot randomly. Then we find all elements which are smaller than the pivot and plug into recursion as list1. Similarly, we find all elements which are greater than the pivot and plug into recursion as list2. Finally, we use list1 + [pivot] + list2 to get a sorted list.
The worst case for quick sort is that it chooses the leftmost element as pivot in every recursive call.If we let T(n) denotes the running time of it. T(n) = O(n) + T(n-1) and we can solve T(n) is O(n^2), which is the same as insertion sort and selection sort. However, in my opinion, we can ignore the worst case for quick sort since it rarely happens. The average case of quick sort takes nlogn time, which is the same as mergesort. We can get this result by solving T(n) = O(n) + 2T(n-1).
When reading my classmate's slog, I found lots of useful information. For example, in Alice's blog (http://lbg940131.blogspot.ca/). She claims that the point of O notation is we need to choose a tight upper bound. Like O(nlogn) is better than 0(n^2) although O(nlogn) is contained in O(n^2). Another classmate gave some helpful reading material about sorting which I found it quite interesting. (http://weidong148.blogspot.ca/)