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.

learn more… | top users | synonyms (2)

166
votes
13answers
20k 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 ...
118
votes
8answers
55k 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 ...
1383
votes
16answers
185k 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.
122
votes
35answers
98k 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 ...
233
votes
31answers
124k 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?
602
votes
24answers
146k 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 ...
108
votes
17answers
71k 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 ...
126
votes
9answers
63k 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 ...
185
votes
13answers
34k 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 = ...
226
votes
21answers
84k 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
21answers
29k 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 ...
55
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 ...
354
votes
52answers
57k 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 ...
231
votes
2answers
29k 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. ...

1 2 3 4 5 299
15 30 50 per page