Questions tagged [algorithm]

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.

4488
votes
37answers
603k views

What is a plain English explanation of “Big O” notation?

I'd prefer as little formal definition as possible and simple mathematics.
1200
votes
17answers
157k views

What is the best algorithm for an overridden System.Object.GetHashCode?

In .NET System.Object.GetHashCode method is used in a lot of places, throughout the .NET base class libraries. Especially when finding items in a collection fast or to determine equality. Is there a ...
741
votes
50answers
469k views

How to count the number of set bits in a 32-bit integer?

8 bits representing the number 7 look like this:00000111Three bits are set. What are algorithms to determine the number of set bits in a 32-bit integer?
498
votes
66answers
387k 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 ...
429
votes
25answers
305k views

What is the 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 like:...
692
votes
34answers
566k views

Calculate distance between two latitude-longitude points? (Haversine formula)

How do I calculate the distance between two points specified by latitude and longitude?For clarification, I'd like the distance in kilometers; the points use the WGS84 system and I'd like to ...
778
votes
22answers
362k 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 ...
461
votes
14answers
94k 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 Order1 'Node 1' 0 102 'Node 1.1' 1 103 'Node 2' 0 ...
423
votes
28answers
424k 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])[1, 2]...
657
votes
10answers
460k views

How to find time complexity of an algorithm

The QuestionHow to find time complexity of an algorithm?What have I done before posting a question on SO ?I have gone through this, this and many other linksBut no where I was able to find a ...
1276
votes
23answers
322k views

What is tail recursion?

Whilst starting to learn lisp, I've come across the term tail-recursive. What does it mean exactly?
592
votes
35answers
889k views

Removing duplicates in lists

Pretty much I need to write a program to check if a list has any duplicates and if it does it removes them and returns a new list with the items that werent duplicated/removed. This is what I have but ...
337
votes
45answers
460k views

Generating all permutations of a given string

What is an elegant way to find all the permutations of a string. E.g. ba, would be ba and ab, but what about abcdefgh? Is there any example Java implementation?
160
votes
21answers
71k views

Unique (non-repeating) random numbers in O(1)?

I'd like to generate unique random numbers between 0 and 1000 that never repeat (i.e. 6 doesn't show up twice), but that doesn't resort to something like an O(N) search of previous values to do it. Is ...
108
votes
18answers
51k views

Rolling or sliding window iterator?

I need a rolling window (aka sliding window) iterable over a sequence/iterator/generator. Default Python iteration can be considered a special case, where the window length is 1. I'm currently using ...
153
votes
12answers
37k 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=ordered.OrderBy(...
340
votes
8answers
244k 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 base.For example: if you want to ...
158
votes
24answers
252k views

Finding all possible combinations of numbers to reach a given sum

How would you go about testing all possible combinations of additions from a given set of numbers so they add up to a given final number?Example:Set of numbers to add: {1,5,22,15,0,...}Desired ...
202
votes
20answers
69k views

Understanding recursion [closed]

I'm having major trouble understanding recursion at school. Whenever the professor is talking about it, I seem to get it but as soon as I try it on my own it completely blows my brains. I was trying ...
205
votes
17answers
164k views

The most efficient way to implement an integer based power function pow(int, int)

What is the most efficient way given to raise an integer to the power of another integer in C?// 2^3pow(2,3)==8// 5^5pow(5,5)==3125
340
votes
41answers
479k views

Best way to reverse a string

I've just had to write a string reverse function in C# 2.0 (i.e. LINQ not available) and came up with this:public string Reverse(string text){char[] cArray=text.ToCharArray();string ...
575
votes
7answers
108k views

What Is Tail Call Optimization?

Very simply, what is tail-call optimization? More specifically, Can anyone show some small code snippets where it could be applied, and where not, with an explanation of why?
196
votes
32answers
199k views

How to find the kth largest element in an unsorted array of length n in O(n)?

I believe there's a way to find the kth largest element in an unsorted array of length n in O(n). Or perhaps it's "expected" O(n) or something. How can we do this?
206
votes
22answers
160k views

How to determine if a point is in a 2D triangle?

Is there an easy way to determine if a point is inside a triangle? It's 2D, not 3D.
135
votes
27answers
116k views

Listing all permutations of a string/integer

A common task in programming interviews (not from my experience of interviews though) is to take a string or an integer and list every possible permutation.Is there an example of how this is done ...
388
votes
10answers
116k views

Why does Java's hashCode() in String use 31 as a multiplier?

In Java, the hash code for a String object is computed ass[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]using int arithmetic, where s[i] is the ith character of the string, n is the length of the ...
115
votes
11answers
82k views

Fastest way to flatten / un-flatten nested JSON objects

I threw some code together to flatten and un-flatten complex/nested JSON objects. It works, but it's a bit slow (triggers the 'long script' warning).For the flattened names I want "." as the ...
267
votes
59answers
265k views

How do you rotate a two dimensional array?

Inspired by Raymond Chen's post, say you have a 4x4 two dimensional array, write a function that rotates it 90 degrees. Raymond links to a solution in pseudo code, but I'd like to see some real world ...
346
votes
5answers
67k views

Constant Amortized Time

What is meant by "Constant Amortized Time" when talking about time complexity of an algorithm?
326
votes
14answers
244k views

Best algorithm for detecting cycles in a directed graph

What is the most efficient algorithm for detecting all cycles within a directed graph?I have a directed graph representing a schedule of jobs that need to be executed, a job being a node and a ...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,