Sorting and Efficiency
The topic about efficiency is what I've been looking forward to for a long time. I feel like I always subconsciously want to make my program run as efficiently as possible with the fewest codes as possible. Usually I'd just sit there for a long time thinking of an efficient method. If I spend too much time, I'd just give up and use the less-efficient method that comes with lots of code.
But I'm pretty sure this applies to most computer scientists. It's as if we're naturally lazy. We want to find the best way to do things, with the least amount of effort. And so, this causes us to come up with methods to code efficiently. I'd be pretty surprised if someone told me recursion wasn't created because of laziness. I mean we could be using loops and nested loops, but we'd have to write a lot of codes and the program could end up being slow. This method is inefficient. Someone must of said "This is enough", and went on and founded recursion. Recursion can drastically cut down the length of your codes.
In computer science, efficiency is measured using Big-Oh. This gives you an upper-bound of the runtime of an algorithm. For example, my problem (before taking this course) would usually be having too many nested loops. The algorithm of a single loop would be O(n). If another loop is nested inside, the algorithm would change into O(n^2). For m number of loops that are all nested within each-other, the algorithm would be O(n^m).
So if n represents the number of items in a list, then as the list doubles, the runtime for the O(n) case will also double. Yes, it may not sound like a problem now but lets go to the O(n^2) case. In this case, the run time would quadrapole as the list doubles. For O(n^3), the run time will be 9x longer. And so for O(n^m), the run time will be 2^m times slower as the list doubles!
As you can see, the more nested loops you have, the longer the runtime. Moreover, your codes would look messier and will be harder to read. Thus your program is inefficient. Both these reasons, especially the latter, is what motivates me in paying attention to the efficiency of my program.
Efficiency appears to mostly discussed and highly relevant with sorting. For a list of 100 items, the sorting run time difference between O(n) and O(n^2) can be a maximum of 99x slower! This is for a hundred items in a list, but it is not uncommon to have to sort between thousands, or even millions of items between a list. And so a difference of one order can make a huge difference in the efficiency of your program!
There are many sorting methods such as selection sort, which gives an efficiency of O(n^2).
When working with efficiency, especially with sorting, it's best to follow this hierarchy below and aim for the smallest Big-Oh algorithm.
O(1) <= O(lgn) <= O(n) <= O(n^2) <= O(n^3) <=.... <= O(2^n) <= O(n^n)