Sunday, 30 March 2014

Week 11: Sorting and Efficiency

Sorting algorithms and its efficiency could be either really challenging or easy depending on how in depth your study is. There are three time complexity cases to focus on; best, average, and worst case. Usually, best cases are ignored because it doesn't tell us anything about the algorithm. It doesn't prepare the programmer for the worst. As for average case, if done properly, could potentially be the most useful because programmers should be expecting anything. However, finding the average case could prove time-consuming and overly-complex because there are unlimited number of assumptions to take into consideration. Thus, most people would save all the trouble and focus on worst case, best known as the Big-Oh Notation. The worst case is used to assess the asymptotic behaviour, as the size of 'x' gets toward the infinity. This is really useful because there are no significant assumptions required and it calculates the worst of the worst, thus, making comparisons between different algorithms really straightforward.

The main sorts that were introduced were quick, merge, and radix sort. I have learned that whenever something is sliced or partitioned in some way in a sorting algorithm, it will often result in some kind of O(lg n). The best way to explain why it will result in some sort of log-n is simply the mathematical reason behind it. Whatever number you sub in for 'n' in log-n, it will 'divide and conquer' so that not every single element would have to be looked at. This makes sorting algorithm like quick and merge (average cases) sort really efficient because their growth rate near infinity is very little compare to, for instance, O(n^2).

Although we do not have to create a sort from scratch, we could always take one and tweak it for optimized efficiency. For instance, the built-in sort in Python is called 'timsort' and it combines a variety of sorts together to create a sort that is very well-rounded. One useful technique that could be used is called 'memoization' and it utilizes a top-down/lazy recursion approach. Without going into heavy details, memoization's purpose is to 'remember' what have been called so redundant function calls could be ignored, thus, trading space-cost for time (which in today's technology, is very worthwhile). For instance, a program that calculates the index of fibonacci using recursion could be made a lot more efficient if the technique of memoization is applied.

No comments:

Post a Comment