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.
179
votes
13answers
22k 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 ...
130
votes
9answers
62k 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 ...
1563
votes
17answers
210k 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
103k 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 ...
242
votes
32answers
131k 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?
643
votes
25answers
158k 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 ...
111
votes
17answers
75k 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 ...
132
votes
9answers
67k 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 ...
201
votes
12answers
37k 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 ...
64
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 = ...
242
votes
22answers
88k 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 ...
59
votes
12answers
14k 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 ...
63
votes
21answers
31k 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 ...
168
votes
7answers
59k 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 ...
84
votes
13answers
69k 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])
...