An algorithm is a sequence of well-defined steps that defines an abstract solution to a problem. Use this tag when your issue is related to algorithm design.
173
votes
13answers
21k views
What is the most efficient/elegant way to parse a flat table into a tree?
Assume you have a flat table that stores an ordered tree hierarchy:
Id Name ParentId Order
1 'Node 1' 0 10
2 'Node 1.1' 1 10
3 'Node 2' 0 ...
122
votes
8answers
58k views
How does Facebook Sharer select Images?
When using Facebook Sharer, Facebook will offer the user the option of using 1 of a few images pulled from the source as a preview for their link. How are these images selected, and how can I ensure ...
1405
votes
16answers
188k views
Plain English explanation of Big O
What is a plain English explanation of Big O? With as little formal definition as possible and simple mathematics.
130
votes
38answers
100k views
Algorithm to return all combinations of k elements from n
I want to write a function that takes an array of letters as an argument and a number of those letters to select.
Say you provide an array of 8 letters and want to select 3 letters from that. Then ...
238
votes
31answers
127k views
How to count the number of set bits in a 32-bit integer?
8 bits representing the number 7 look like this:
00000111
Three bits are set. What are algorithms to determine the number of set bits in a 32-bit integer?
625
votes
24answers
152k views
How to create a GUID / UUID in Javascript?
I'm trying to create globally-unique identifiers in Javascript. I'm not sure what routines are available on all browsers, how "random" and seeded the built-in random number generator is, etc..
The ...
109
votes
17answers
73k views
Most effective way for float and double comparison
What would be the most efficient way to compare two doubles or two floats (single precision)?
Simply doing this is not correct:
bool CompareDoubles1 (double A, double B)
{
return A == B;
}
But ...
129
votes
9answers
65k views
Image comparison - fast algorithm
I'm looking to create a base table of images and then compare any new images against that to determine if the new image is an exact (or close) duplicate of the of the base. For example: if you want ...
191
votes
12answers
35k views
Determine Whether Two Date Ranges Overlap
Given two date ranges, what is the simplest or most efficient way to determine whether the two date ranges overlap?
As an example, suppose we have ranges denoted by DateTime variables StartDate1 to ...
62
votes
8answers
12k views
Is using Random and OrderBy a good shuffle algorithm?
I have read an article about various shuffle algorithms over at Coding Horror. I have seen that somewhere people have done this to shuffle a list:
var r = new Random();
var shuffled = ...
237
votes
22answers
86k views
Big O, how do you calculate/approximate it?
Most people with a degree in CS will certainly know what Big O stands for.
It helps us to measure how (in)efficient an algorithm really is and if you know in what category the problem you are trying ...
57
votes
12answers
13k views
Unique random numbers in O(1)?
The problem is this: I'd like to generate unique random numbers between 0 and 1000 that never repeat (I.E. 6 doesn't come out twice), but that doesn't resort to something like an O(N) search of ...
60
votes
21answers
30k views
Detecting endianness programmatically in a C++ program
Is there a programmatic way to detect whether or not you are on a big-endian or little-endian architecture? I need to be able to write code that will execute on an Intel or PPC system and use exactly ...
79
votes
13answers
67k views
How to generate all permutations of a list in Python
How do you generate all the permutations of a list in Python, independently of the type of elements in that list.
For example:
permutations ([])
[]
permutations ([1,])
[1]
permutations ([1,2])
...
161
votes
7answers
56k views
Efficient equivalent for removing elements while iterating the Collection
We all know you can't do this:
for (Object i : l) {
if (condition(i))
l.remove(i);
}
ConcurrentModificationException etc... this apparently works sometimes, but not always. Here's some ...