Most Popular

1500 questions
386
votes
12 answers

Why is quicksort better than other sorting algorithms in practice?

In a standard algorithms course we are taught that quicksort is $O(n \log n)$ on average and $O(n^2)$ in the worst case. At the same time, other sorting algorithms are studied which are $O(n \log n)$ in the worst case (like mergesort and heapsort),…
Janoma
  • 5,535
  • 3
  • 20
  • 21
328
votes
7 answers

What is the definition of P, NP, NP-complete and NP-hard?

I'm in a course about computing and complexity, and am unable to understand what these terms mean. All I know is that NP is a subset of NP-complete, which is a subset of NP-hard, but I have no idea what they actually mean. Wikipedia isn't much help…
Mirrana
  • 4,299
  • 5
  • 21
  • 21
218
votes
32 answers

Why is writing down mathematical proofs more fault-proof than writing computer code?

I have noticed that I find it far easier to write down mathematical proofs without making any mistakes, than to write down a computer program without bugs. It seems that this is something more widespread than just my experience. Most people make…
user56834
  • 3,722
  • 4
  • 18
  • 32
195
votes
3 answers

Is there a system behind the magic of algorithm analysis?

There are lots of questions about how to analyze the running time of algorithms (see, e.g., runtime-analysis and algorithm-analysis). Many are similar, for instance those asking for a cost analysis of nested loops or divide & conquer algorithms,…
Raphael
  • 72,336
  • 29
  • 179
  • 389
194
votes
10 answers

How can a language whose compiler is written in C ever be faster than C?

Taking a look at Julia's webpage, you can see some benchmarks of several languages across several algorithms (timings shown below). How can a language with a compiler originally written in C, outperform C code? Figure: benchmark times relative to…
StrugglingProgrammer
  • 1,983
  • 3
  • 11
  • 10
182
votes
13 answers

Why, really, is the Halting Problem so important?

I don't understand why the Halting Problem is so often used to dismiss the possibility of determining whether a program halts. The Wikipedia article correctly explains that a deterministic machine with finite memory will either halt or repeat a…
Brent
  • 2,553
  • 3
  • 14
  • 22
146
votes
3 answers

How can it be decidable whether $\pi$ has some sequence of digits?

We were given the following exercise. Let $\qquad \displaystyle f(n) = \begin{cases} 1 & 0^n \text{ occurs in the decimal representation of } \pi \\ 0 & \text{else}\end{cases}$ Prove that $f$ is computable. How is this possible? As far as I know,…
Raphael
  • 72,336
  • 29
  • 179
  • 389
145
votes
12 answers

Why are there so many programming languages?

I'm pretty fluent in C/C++, and can make my way around the various scripting languages (awk/sed/perl). I've started using python a lot more because it combines some of the nifty aspects of C++ with the scripting capabilities of awk/sed/perl. But why…
Suresh
  • 5,387
  • 2
  • 27
  • 31
141
votes
5 answers

How to convert finite automata to regular expressions?

Converting regular expressions into (minimal) NFA that accept the same language is easy with standard algorithms, e.g. Thompson's algorithm. The other direction seems to be more tedious, though, and sometimes the resulting expressions are…
Raphael
  • 72,336
  • 29
  • 179
  • 389
137
votes
7 answers

Is Category Theory useful for learning functional programming?

I'm learning Haskell and I'm fascinated by the language. However I have no serious math or CS background. But I am an experienced software programmer. I want to learn category theory so I can become better at Haskell. Which topics in category…
dan
136
votes
3 answers

BIT: What is the intuition behind a binary indexed tree and how was it thought about?

A binary indexed tree has very less or relatively no literature as compared to other data structures. The only place where it is taught is the topcoder tutorial. Although the tutorial is complete in all the explanations, I cannot understand the…
Nikunj Banka
  • 1,535
  • 3
  • 11
  • 9
132
votes
14 answers

Why can I look at a graph and immediately find the closest point to another point, but it takes me O(n) time through programming?

Let me clarify: Given a scatterplot of some given number of points n, if I want to find the closest point to any point in the plot mentally, I can immediately ignore most points in the graph, narrowing my choices down to some small, constant number…
Ari
  • 1,417
  • 2
  • 10
  • 5
128
votes
15 answers

How to fool the "try some test cases" heuristic: Algorithms that appear correct, but are actually incorrect

To try to test whether an algorithm for some problem is correct, the usual starting point is to try running the algorithm by hand on a number of simple test cases -- try it on a few example problem instances, including a few simple "corner cases". …
D.W.
  • 159,275
  • 20
  • 227
  • 470
127
votes
6 answers

Why hasn't there been an encryption algorithm that is based on the known NP-Hard problems?

Most of today's encryption, such as the RSA, relies on the integer factorization, which is not believed to be a NP-hard problem, but it belongs to BQP, which makes it vulnerable to quantum computers. I wonder, why has there not been an encryption…
Ken Li
  • 3,078
  • 3
  • 23
  • 38
127
votes
5 answers

What's the difference between a binary search tree and a binary heap?

These two seem very similar and have almost an identical structure. What's the difference? What are the time complexities for different operations of each?
Piper
  • 1,759
  • 2
  • 12
  • 13
1
2 3
99 100