The day before the test I read this table for preparation:
(http://bigocheatsheet.com/)
What we've always been focusing on is the worst case performance of an algorithm. By intuition, the "worst case" a sorting algorithm could have should be when the given list is in descending order (i.e. sorted and reversed, as described in the lab this week). In the worst case, merge sort performs well, while bubble, insertion and selection we learned from CSC108 has less efficient algorithms.
How efficiently a function works is determined by the way it does the work and how long the computer actually takes to finish each step. Even a small variance in the code may have a big influence on the overall efficiency. For example, if you have a while loop, and have some value like L[i:] as the condition, the function would have to take the piece L[i:] each time when the loop is executed. If there could be an alternative version of this while loop which uses the single index i as the variant to be examined as the condition, it would make this part of the code much more efficient.
Personally I like selection sort, though it is not an efficient one. No matter how the list is arranged (random, sorted, or sorted and reversed), in each step of iteration it looks at every element in the list and find the smallest one, then switch it with the first element in the unsorted part. Even when the rest of the list is already sorted, there is no way the function could know it. That's why it has the same big O for its worst case and best case. This sorting algorithm works in a way closest to how a human's brain processes list while manually sorting a list.
Another algorithm which has the same efficiency working in its best case and worst case is merge sort. When given a list, the function breaks it into two parts, repeat these steps until there are only two elements in a group so that this group can be merged together in order. Then it merges each group of two lists, until there is one final ordered list left. Even if it is originally given a completely sorted list, it will still do these steps since it does not know the list given is already its correct order.
Merge sort is more efficient than selection sort just like binary search is more efficient than linear search. By dividing the list in halves it can actually save lots of calculation.
Bubble sort and insertion sort are, though doing bad in the worst cases, doing better than merge sort in the best cases, when the given list is already sorted. The point is that these two algorithms do examine whether each number is in their correct position, and if so they just go through the entire list and then end without doing anything else. In this way they win the race when the list given is already sorted.
In most cases, however, the list given to sort is a random list, so that the average and worst-case run-time is more important when we discuss the efficiency. So from the list above, we consider merge sort and radix sort as efficient algorithms.

No comments:
Post a Comment