Hashtable in C++?

If you’re using C++11, you have access to the <unordered_map> and <unordered_set> headers. These provide classes std::unordered_map and std::unordered_set. If you’re using C++03 with TR1, you have access to the classes std::tr1::unordered_map and std::tr1::unordered_set, using the same headers (unless you’re using GCC, in which case the headers are <tr1/unordered_map> and <tr1/unordered_set> instead). In all cases, … Read more

why does accessing an element in an array take constant time?

The array, effectively, is known by a memory location (a pointer). Accessing a[3] can be found in constant time, since it’s just location_of_a+3*sizeof(int). In C, you can see this directly. Remember, a[3] is the same as *(a+3) – which is a bit more clear in terms of what it’s doing (dereferencing the pointer “3 items” … Read more

Can an O(n) algorithm ever exceed O(n^2) in terms of computation time?

Asymptotic complexity (which is what both big-O and big-Theta represent) completely ignores the constant factors involved – it’s only intended to give an indication of how running time will change as the size of the input gets larger. So it’s certainly possible that an Θ(n) algorithm can take longer than an Θ(n2) one for some … Read more

Sorting algorithms for data of known statistical distribution?

If the data you are sorting has a known distribution, I would use a Bucket Sort algorithm. You could add some extra logic to it so that you calculated the size and/or positions of the various buckets based upon properties of the distribution (ex: for Gaussian, you might have a bucket every (sigma/k) away from … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)