Quick sort Worst case

Quicksort’s performance is dependent on your pivot selection algorithm. The most naive pivot selection algorithm is to just choose the first element as your pivot. It’s easy to see that this results in worst case behavior if your data is already sorted (the first element will always be the min). There are two common algorithms … Read more

Asymptotic complexity of .NET collection classes

MSDN Lists these: Dictionary<,> List<> SortedList<,> (edit: wrong link; here’s the generic version) SortedDictionary<,> etc. For example: The SortedList(TKey, TValue) generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedDictionary(TKey, TValue) generic class. The two classes … Read more

How come list element lookup is O(1) in Python?

A list in Python is implemented as an array of pointers1. So, what’s really happening when you create the list: [“perry”, 1, 23.5, “s”] is that you are actually creating an array of pointers like so: [0xa3d25342, 0x635423fa, 0xff243546, 0x2545fade] Each pointer “points” to the respective objects in memory, so that the string “perry” will … Read more

What is the difference between O, Ω, and Θ?

It is important to remember that the notation, whether O, Ω or Θ, expresses the asymptotic growth of a function; it does not have anything intrinsically to do with algorithms per se. The function in question may be the “complexity” (running time) of an algorithm, either worst-case, best-case or average-case, but the notation is independent … Read more

What is the complexity of this simple piece of code?

This seems to be a question of mislead, because I happened to read that book just now. This part of text in the book is a typo! Here is the context: =================================================================== Question: What is the running time of this code? 1 public String makeSentence(String[] words) { 2 StringBuffer sentence = new StringBuffer(); 3 for … Read more

C# List remove from end, really O(n)?

In general List<T>::RemoveAt is O(N) because of the need to shift elements after the index up a slot in the array. But for the specific case of removing from the end of the list no shifting is needed and it is consequently O(1)