Running time of algorithm A is at least O(n²) – Why is it meaningless?

T(n): running time of Algo A. We just care about the upper bound and lower bound of T(n) The statement: T(n) >= O(n^2) Upper bound: Because T(n) >= O(n^2), then there’s no information about upper bound of T(n) Lower bound: Assume f(n) = O(n^2), then the statement: T(n) >= f(n), but f(n) could be anything that is “smaller” than n^2 , Ex: constant, n,…, So there’s no conclusion … Read more

What’s the best way to dedupe a table?

SELECT DISTINCT <insert all columns but the PK here> FROM foo. Create a temp table using that query (the syntax varies by RDBMS but there’s typically a SELECT … INTO or CREATE TABLE AS pattern available), then blow away the old table and pump the data from the temp table back into it.

Best way to randomize an array with .NET

If you’re on .NET 3.5, you can use the following IEnumerable coolness: Edit: and here’s the corresponding VB.NET code: Second edit, in response to remarks that System.Random “isn’t threadsafe” and “only suitable for toy apps” due to returning a time-based sequence: as used in my example, Random() is perfectly thread-safe, unless you’re allowing the routine … Read more

About bubble sort vs merge sort

It’s a trick question. If you just want the maximum, (or indeed, the kth value for any k, which includes finding the median), there’s a perfectly good O(n) algorithm. Sorting is a waste of time. That’s what they want to hear. As you say, the algorithm for maximum is really trivial. To ace a question like this, you … Read more

Minesweeper solving algorithm

I am pretty sure most of you know about the minesweeper game. I wanted to code (in C#) my own minesweeper game and was looking for some input as to what would be a good algorithm for that game. I have been browsing over the web for quite some time now but could not find … Read more

Finding square root without using sqrt function?

There is a better algorithm, which needs at most 6 iterations to converge to maximum precision for double numbers: Algorithm starts with 1 as first approximation for square root value. Then, on each step, it improves next approximation by taking average between current value y and x/y. If y = sqrt(x), it will be the same. If y > sqrt(x), then x/y < sqrt(x) by about the same … Read more