Okay so…first of
all I would like to take a moment to acknowledge the fact that this is my last
Slog post for this year *cry*.
It’s been a pretty
good semester and I definitely had fun in CSC148, and most of all, I learnt
more about the wonderful world of Python Programming!
So in this post I
will be finishing things off by explaining Sorting and Efficiency!
What’s sorting?
Most of you
probably know what sorting is in general terms. You take a list like this: [1,
3, 5, 6, 2] and if you sorted it so that it was in ascending order, you’d want
this list to be returned: [1, 2, 3, 5, 6]. However, there are many ways to
actually code an algorithm that can actually sort a list. Here are the main
ones we focused on this semester:
Bubble sort
Merge Sort
Quick Sort
Built-in sort
Most of the
descriptions for these sorts can be found here and in my previous blog post.
What’s efficiency?
So with all these
different sorts, how do we know which one is the best? We find this out by
counting how many steps each different type of sort takes sort a list of length
n. In other terms, we’re calculating its runtime. Since I took CSC165 last
year, calculating runtime wasn’t a new concept to me, and I was pretty familiar
with it. When counting the number of steps an algorithm takes to sort a list,
you usually count how many comparisons are made. Normally, this is also done
with the worst-case possible, since it gives an upper bound to the run-time (in
other words, the maximum possible runtime). We write this by using Big-O
notation. Here’s a graph showing Big-O runtime of different functions:
For example, if we
had a list which was in descending (worst
case) order, and we wanted to sort
it into ascending order using bubble
sort, this would give us a runtime of O(n^2). This means that the maximum
possible runtime would be a factor of n^2. And as expected, if you have a
longer list, more comparisons would be made, which would mean the runtime also
increases. Another example is merge
sort, which, as I stated in my last post, has a runtime of O(nlogn) since you
keep splitting the list in half.
So if an algorithm
has a faster runtime, then it’s more efficient, which makes it a lot better,
and obviously, as programmers, we want efficient algorithms which will solve
problems for us quickly!
That’s all for now!
Good luck on the final everyone!