Last Week:
On Sorting:
I learned a lot more about run-time complexity at yesterday's lecture.Assume there are 2 functions, f1 and f2 that are O(n^2) and O(nlogn) respectively. Consider 2 compositions, g(s) = f`1(f2(s)) and h(t) as f2(f1(t)).
We can analyze minimum and maximum runtimes, assuming we can choose inputs t and s. This I didn't know: I thought that regardless of the kind of objects being operated on, the run-time would be the same. Now I understand why it's specifically called worst-case. Anyways: for the minimum(or best case scenario), just choose the easiest input possible (like anything returning bools after simple linear operations, or even just anything returning argument t or s).
If we further assume that both f1 and f2 perform some sort of str to str conversion (the specific code doesn't matter), we know that for g, f2 will give an output of n^2 (another thing I learned, that O applies to both complexity for runtime and output size), and altogether, as nlogn is faster than O(n^2), the overall runtime cannot be less than n^2. For the worst case, it will look like a regular composition, or be 2n^2logn.
A similar analysis can be performed for h, but the key is that I learned a proper technique for evaluating these type of things: I can vary inputs depending on which scenario I am concerned with, and that the overall output will not always be an arithmetic composition. I have to think about how the code works: if there are 2 operations, and are they are performed consequently, for a worst case the overall run-time will be the sum, as in any case the program must execute both. Also, given 2 different Os for such consequent operations, the asymptotic runtime(which is really "run-time in the limit", or "where run-time will even out") will be dominated by the longer time, as for the above example, nlogn is a negligible addition to n^2.
Lastly, this type of analysis can give much more tighter bounds than what I was doing before. In any case, I will pay most attention to whatever Big O practice is posted as this is my weakest area by far.
On Sorting:
I learned a lot more about run-time complexity at yesterday's lecture.Assume there are 2 functions, f1 and f2 that are O(n^2) and O(nlogn) respectively. Consider 2 compositions, g(s) = f`1(f2(s)) and h(t) as f2(f1(t)).
We can analyze minimum and maximum runtimes, assuming we can choose inputs t and s. This I didn't know: I thought that regardless of the kind of objects being operated on, the run-time would be the same. Now I understand why it's specifically called worst-case. Anyways: for the minimum(or best case scenario), just choose the easiest input possible (like anything returning bools after simple linear operations, or even just anything returning argument t or s).
If we further assume that both f1 and f2 perform some sort of str to str conversion (the specific code doesn't matter), we know that for g, f2 will give an output of n^2 (another thing I learned, that O applies to both complexity for runtime and output size), and altogether, as nlogn is faster than O(n^2), the overall runtime cannot be less than n^2. For the worst case, it will look like a regular composition, or be 2n^2logn.
A similar analysis can be performed for h, but the key is that I learned a proper technique for evaluating these type of things: I can vary inputs depending on which scenario I am concerned with, and that the overall output will not always be an arithmetic composition. I have to think about how the code works: if there are 2 operations, and are they are performed consequently, for a worst case the overall run-time will be the sum, as in any case the program must execute both. Also, given 2 different Os for such consequent operations, the asymptotic runtime(which is really "run-time in the limit", or "where run-time will even out") will be dominated by the longer time, as for the above example, nlogn is a negligible addition to n^2.
Lastly, this type of analysis can give much more tighter bounds than what I was doing before. In any case, I will pay most attention to whatever Big O practice is posted as this is my weakest area by far.