Sorting
- The selection sort – for each element – finds the minimum value from that element onwards, and switches the two elements (this is for sorting in ascending order, of course).
- The merge sort, on the other hand, continually halves an unsorted list (recursion’s a great tool!), then sorts those “halves” and then finally combines all of these halves to create a sorted list.
- The quick sort chooses a “pivot”
- decide wherer the povot goes with respect to the rest of the list
- repeat on partition
thus, i understand that in order to become a good programmer, it is far from enough to just being able to write a programmable code, but also need to efficient, to work out the problem more quickly than others.
In class, we compared each of the various sorts on different input sizes. the selection and merge sorts became inefficient quickly, while it was the quick sort that came out on top (of the sorts we studied); this didn’t surprise me one bit because its Big-O efficiency of O(n log n). But, it turned out that built-in list.sort() came up the winner in all but the very biggest inputs,due to its searching mechanism itself changes for different input sizes.
log n is the number of times you can divide n in half before reading 1
also the type of input is another essential factor in running efficiently.
from the angle of efficiency, Danny mainly talks about BIG-O efficiency.
Big-O running time is an upper-bound on the running-time of a given algorithm, and gives the worst-case performance of an algorithm. This worst-case performance is given in terms of the input size for the algorithm.( the Big-O efficiency of a linear search is O(n), or “Oh-n”,meaning that, at worst, the algorithm need to scan all n elements in a list to find a given element.)
Selection sort: O(n^2)Merge Sort: O(n log n)Quick Sort: O(n log n) — both merge and quick sorts may have the same Big-O efficiency. However , when we run into large input, the merge sort’s efficiency becomes closer to O(n),worse than the quick sort’s O(n log n)
No comments:
Post a Comment