Tuesday, April 1, 2014

Sorting and Efficiency

         From the primary course CSC165, I have learned the way to calculate efficiency of a function using Big-O. Furthermore, I learned and applied for using built-in sort in course CSC108. Also, I have known other three types of sort: the bubble sort, the selection sort and the insertion sort, especially their differences.
         In the course CSC148, I systematically learned more types sort included the kinds I learned before and efficiency.  Firstly, I would like to talk about what Sorting is. Sorting is the process that places the items into the given order by obvious characteristics. Then, I want to analyze several sorting techniques and their running time.
         The Bubble Sort takes n-1 passes through a list. In each pass, it compares the item starting at first one to the next item. If the previous one is bigger than the next, the two items exchange; otherwise, there is no exchange. Throughout the one pass, the last position can be determined, which the largest item in. The following pass will determine the second largest item. Thus, the first pass makes n-1 comparisons and the second pass makes n-2 comparison.  The sum of the first n-1 integers is 1/2n2 – 1/2n, which is the total number of comparisons. The efficiency is O(n2). The best case is that the list is already sorted, while the worse case is that every comparison leads to the exchange.
         The Selection Sort makes the same number of comparisons with the Bubble Sort, but the number of exchange is apparently smaller than the number in the Bubble Sort because there is only one exchange in each pass, which exchange the largest item and the item in proper position. For instance, the second largest item in the list should swap with the second last item. Shortly, the Selection is also O(n2).
         The Insertion Sort determines exactly one item which follows the sublist to the sorted sublist that is in the lower positions of the list. Thus, the first pass makes only one comparison and last pass makes n-1 comparison. We can know that the Insertion Sort is still O(n2). 
         The Shell Sort is an improved technique of the Insertion Sort. It creates several sublists by using an increment i. It will have more sorted list through the passes and complete the insertion at the last step with few comparisons. Because it is more efficient than the Insertion Sort, the running time of the Shell Sort is between O(n) and O(n2), specifically, it is O(n3/2).
         The Merge Sort is a recursive algorithm that splits a list in half until the list is empty or only one item(log n times). Sorting the base case first and combining them together each time when finishing the completed list. The total running time could be O(nlog n).
         The Quick Sort usually takes the first item as pivot value and divides the list by split point that is the position the pivot value belongs. Also it named quick sort, but the running time is O(n2), like others.
         I am more familiar with these several sorting process with their efficiencies during the study of this week. I know the best and worse case. I also know how to write the functions of these distinct techniques. All in all, these algorithms are important in studying Computer Science and I want to learn them well.