donderdag 28 juni 2012

QuickSort

The next algorithm the course required us to implement is QuickSort. QuickSort is quite a well known algorithm and, theoretically speaking, it performs as good as Merge Sort ( O(n log n) time ). In practice however, it usually comes out faster. Choosing the pivot well is essential to a good running time.
Here is my implementation, with the pivot choosing subroutine left out:



In my next post I will show how we can choose a good pivot and how much it matters.

dinsdag 26 juni 2012

Selection Sort

I decided to throw another sorting algorithm into the mix: Selection Sort.
In theory, this one should be the worst of all.
In practice, it turned out a little bit different:

Online Graphing
Create a graph
This was my implementation:

When we analyze these algorithms mathematically, we can conclude that:

  • Merge Sort runs in O(n log n) time.
  • Both selection and insertion sort run in O(n²) time.
As you can see, this makes a huge difference when amount of data increases.

maandag 25 juni 2012

Insertion Sort

In order to understand just how important a good sorting algorithm is, I quickly whipped up an Insertion Sort:


I applied both of them on a list of 100,000 numbers. This was the result, in seconds: Online Graphing
Graphing

zondag 24 juni 2012

Merge Sort

The first programming challenge the course over at Coursera provided us with was to implement an algortihm for counting the inverions in an array. As it turns out, this can be achieved by applying a minor modification in the Merge Sort algorithm. I implemented it both in C++ and Python.
Python version C++ version I'll leave the method to calculate the inversions as an exercise for the reader, it's really not that difficult.

zaterdag 23 juni 2012

Coursera

The finals are finally over! I'm so happy I can get back to work on my projects. As I mentioned in my last post I'll be revealing my latest project soon, on this blog. I'm also taking up a free course offered by Stanford University on Coursera. the "Design and Analysis of Algorithms" course will undoubtedly sharpen my skills regarding algorithms and will make me a better programmer in general. You can still sign up now!