recursion versus iteration

Recursion is usually much slower because all function calls must be stored in a stack to allow the return back to the caller functions. In many cases, memory has to be allocated and copied to implement scope isolation. Some optimizations, like tail call optimization, make recursions faster but aren’t always possible, and aren’t implemented in … Read more

Quicksort: Choosing the pivot

Choosing a random pivot minimizes the chance that you will encounter worst-case O(n2) performance (always choosing first or last would cause worst-case performance for nearly-sorted or nearly-reverse-sorted data). Choosing the middle element would also be acceptable in the majority of cases. Also, if you are implementing this yourself, there are versions of the algorithm that … Read more

Polynomial time and exponential time

Below are some common Big-O functions while analyzing algorithms. O(1) – constant time O(log(n)) – logarithmic time O((log(n))c) – polylogarithmic time O(n) – linear time O(n2) – quadratic time O(nc) – polynomial time O(cn) – exponential time O(n!) – factorial time (n = size of input, c = some constant) Here is the model graph … Read more