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.
191
votes
13answers
23k 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 ...
140
votes
9answers
69k 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 ...
1647
votes
16answers
218k 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.
140
votes
40answers
107k 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 ...
255
votes
32answers
137k 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?
120
votes
17answers
79k views
Most effective way for float and double comparison
What would be the most efficient way to compare two double or two float values?
Simply doing this is not correct:
bool CompareDoubles1 (double A, double B)
{
return A == B;
}
But something ...
139
votes
9answers
70k 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 ...
212
votes
12answers
40k 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 ...
68
votes
8answers
13k 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 = ...
251
votes
21answers
92k 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 ...
180
votes
7answers
64k 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 ...
61
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 ...
68
votes
21answers
33k 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 ...
246
votes
2answers
32k views
What's the Hi/Lo algorithm?
What's the Hi/Lo algorithm?
I've found this in the NHibernate documentation (it's one method to generate unique keys, section 5.1.4.2), but I haven't found any good explanation of how does it work.
...
378
votes
52answers
62k views
Expand a random range from 1–5 to 1–7
Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.
What is a simple solution?
What is an effective solution ...