Saturday, 29 March 2014

Week 11: Sorting and Efficiency

Efficiency is a sought after aspect of almost any action in life. The implementer always wants to ensure the task that they are trying to complete is done in the fastest and most efficient way possible. In computer science it is the same feeling towards programs and functions. A computer scientist wants to make sure that the algorithm they are creating will work efficiently. Sorting algorithms are a good learning tool for observing how time complexity and efficiency are involved in a program. In this weeks lab we did several time tests on various sorting functions such as; Pythons tim sort, quick sort, merge sort, bubble sort, selection sort and many others. we observed the sped of the functions with 3 different inputs from best case to work case. Best case being a sorted list to sort. Worst case being an unsorted reversed list. And the average case being a random list. We saw as various functions worked well with different inputs but also how some other functions decreased in efficiency tremendously. 

The different sorts in the lab all had different O(n) values. O(n) is a term made to represent the limiting value and speed in which a functions divides tasks. Selection sort would have a run time of O(n^2), as it compares every item with every other item in the list to see what element s the smallest. Insertion sort has the same O(n) value because it needs to compare each element with every other item in the list. The run time of bubble sort is also O(n^2), because it compares every possible pair in the list. Quick sort has a value of O(nlgn), although the worst run time is still O(n^2), the function partitions it into two lists continuously,  while returning a number each iteration. Python's Tim sort has a run time of O(nlgn), which it works out through adaptive sorting methods.

Memoization was also discussed in class a way a function could be slower than it could possibly be. This is because of redundant recursive calls such as seen in the fibonacci sequence which calls f(n-2) two times(f(n-l) same value is on next recursive iteration). This is a waste of a call and also a waste of time. For these reasons we analyze programs and their efficiency to develop products that are the best they possibly can be. Sorting has shown me how and why efficiency is so very important as a CS subject.

No comments:

Post a Comment