This weeks topic is arguably one of the most important aspects of computer science. The goal of computer scientists should be to create programs, algorithms and software that perform at an optimal rate in order to reduce the amount of time and money required to achieve the desired result. One category of algorithms that are linked very closely to the concept of efficiency are sorting algorithms. Sorting algorithms consist of various different approaches that lead to the same result; a sorted array of data. Now, the question that some people might be asking is: if they all lead to the same conclusion, why do we need to have all these different types of sorting algorithms? The answer, as you might expect, is that they differ in their sorting efficiency.
Different tasks have different thresholds that a programmer has to meet (some might be stricter, some might be more lenient), and that allows the programmer to choose the most efficient sorting algorithm that suits their needs. For example, if your employer asked you to write a program that sorts an array in maximum x units of time. The keyword here is maximum, meaning that the programmer would need to go through all of the sorting algorithms he knows and choose the few that perform the task in less than or equal to the amount of time that the employer asked for. This requirement is referred to as the "worst-case run-time". This requirement of maximum run-time can be measured with a concept called Big O notation.
Big O notation is a type of notation that gives us information about the performance of an algorithm with a specific input size of n. Big O notation is basically a bound on a function, restricting its behavior. Other bounds include Big Theta and Big Omega, however, we're only interested in Big O right now. Big O is the upper bound on a function, meaning that in any specific case, the function can not grow faster than the Big O. One important thing to note here is that saying an algorithm runs in Big O of a function does not automatically mean it is the worst-case run-time. A case is not the same as a bound. It is also possible to measure the average-case and best-case in terms of Big O.
If an algorithms run-time is in Big O of a function, then its run-time will never exceed the function. To relate this to sorting algorithms, there are various values of Big O that a sorting algorithm may have. For these examples, we'll be looking at the average-case time complexities, for comparisons sake. First of all, O(1) is used to describe a sorting algorithm that's run-time never changes regardless of the input size. We have not really had any sorting algorithms of this efficiency introduced to us yet, but I'm sure some exist. Next, we have O(n^2) algorithms, such as insertion sort, selection sort, bubblesort which are not used very often, as they are not very efficient. Then, we have the O(n logn) algorithms which are generally the most used, such as mergesort, quicksort and heapsort.
In conclusion, it is important to keep in mind that as computer scientists and programmers, we can reduce the amount of time and money being invested in simple operations such as sorting by simply choosing the right algorithm that maximizes the efficiency. Going forward, the knowledge about efficiency we've learned so far will be crucial in creating programs that perform as nicely as possible, and keep our code as clean as can be.
No comments:
Post a Comment