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.

An algorithm is a set of ordered instructions based on a formal language with the following conditions:

  • Finite. The number of instructions must be finite.
  • Executable. All instructions must be executable in some language-dependent way, in a finite amount of time.

An algorithm does not have to be deterministic - there are many random-based algorithms, e.g. QuickSort with selecting the pivot element randomly.

An algorithm can be expressed in many ways:

  • As a sequence of instructions
  • As a block-scheme
  • As a code in an existing or new programming language
  • As a piece of text in a human language
  • As a realization in a formal model of computation such as a Turing machine
  • Or in other, similar ways

An algorithm can solve a class of problems. For example, both "sum 1 and 3" and "sum 4 and 5" are problems in the same class: "sum two integer numbers." Furthermore, a given class of problems can generally be solved by a variety of algorithms.

One important aspect of algorithm design is performance. For large datasets, a good algorithm may outperform a poor algorithm by several orders of magnitude. Algorithm performance is often rated with Big O or Θ notation, but one should be cautious with asymptotic notation, since big constants may be involved.

A key algorithm classification is known as Algorithm Complexity.

Another important step of algorithm design is proof of correctness. The algorithm should actually provide a correct result for any instance of the problem. We should always take care of all the conditions where the proposed algorithm might fail. This could be done using several techniques, For example, Model Checking, Assertion, Loop Invariant.

Related Links

Additional resources on algorithms include:

119388 questions
5342
votes
43 answers

What is a plain English explanation of "Big O" notation?

I'd prefer as little formal definition as possible and simple mathematics.
4850
votes
62 answers

How do I check if an array includes a value in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++) { if (a[i] === obj) { return…
brad
  • 73,826
  • 21
  • 73
  • 85
4170
votes
38 answers

How can I pair socks from a pile efficiently?

Yesterday I was pairing the socks from the clean laundry and figured out the way I was doing it is not very efficient. I was doing a naive search — picking one sock and "iterating" the pile in order to find its pair. This requires iterating over n/2…
amit
  • 175,853
  • 27
  • 231
  • 333
2690
votes
32 answers

What does O(log n) mean exactly?

I am learning about Big O Notation running times and amortized times. I understand the notion of O(n) linear time, meaning that the size of the input affects the growth of the algorithm proportionally...and the same goes for, for example, quadratic…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
2057
votes
14 answers

What is the optimal algorithm for the game 2048?

I have recently stumbled upon the game 2048. You merge similar tiles by moving them in any of the four directions to make "bigger" tiles. After each move, a new tile appears at random empty position with a value of either 2 or 4. The game terminates…
nitish712
  • 19,504
  • 5
  • 26
  • 34
2033
votes
29 answers

What is tail recursion?

Whilst starting to learn lisp, I've come across the term tail-recursive. What does it mean exactly?
1890
votes
23 answers

Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

One of the most interesting projects I've worked on in the past couple of years was a project about image processing. The goal was to develop a system to be able to recognize Coca-Cola 'cans' (note that I'm stressing the word 'cans', you'll see why…
Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
1666
votes
22 answers

What is the best algorithm for overriding GetHashCode?

In .NET, the GetHashCode method is used in a lot of places throughout the .NET base class libraries. Implementing it properly is especially important to find items quickly in a collection or when determining equality. Is there a standard algorithm…
bitbonk
  • 48,890
  • 37
  • 186
  • 278
1435
votes
57 answers

Removing duplicates in lists

How can I check if a list has any duplicates and return a new list without duplicates?
Neemaximo
  • 20,031
  • 12
  • 32
  • 40
1275
votes
49 answers

Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing

I had an interesting job interview experience a while back. The question started really easy: Q1: We have a bag containing numbers 1, 2, 3, …, 100. Each number appears exactly once, so there are 100 numbers. Now one number is randomly picked out of…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
1222
votes
7 answers

Ukkonen's suffix tree algorithm in plain English

I feel a bit thick at this point. I've spent days trying to fully wrap my head around suffix tree construction, but because I don't have a mathematical background, many of the explanations elude me as they start to make excessive use of mathematical…
Nathan Ridley
  • 33,766
  • 35
  • 123
  • 197
1143
votes
49 answers

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 understand the relative accuracies of the approaches…
Robin Minto
  • 15,027
  • 4
  • 37
  • 40
1076
votes
10 answers

What is tail call optimization?

Very simply, what is tail-call optimization? More specifically, what are some small code snippets where it could be applied, and where not, with an explanation of why?
1036
votes
10 answers

How can I find the time complexity of an algorithm?

I have gone through Google and Stack Overflow search, but nowhere I was able to find a clear and straightforward explanation for how to calculate time complexity. What do I know already? Say for code as simple as the one below: char h = 'y'; // This…
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
1007
votes
65 answers

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 the algorithms to determine the number of set bits in a 32-bit integer?
Matt Howells
  • 40,310
  • 20
  • 83
  • 102
1
2 3
99 100