Questions tagged [algorithm-analysis]

Questions about the science and art of determining properties of algorithms, often including correctness, runtime and space usage. Use the [runtime-analysis] tag for questions about the runtime of algorithms.

This question is for all aspects of analysis of algorithms, including their correctness, their running time, their space usage, or other properties.

If you are asking specifically about the running time of an algorithm, might be more appropriate.

We have several reference questions that provide useful material about the analysis of algorithms:

Many elementary questions about algorithm analysis can be solved using the techniques in those reference questions, so you are encouraged to check whether your problem can be solved using those standard techniques before asking.

2059 questions
196
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,…
132
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.
  • 170,893
  • 23
  • 241
  • 528
87
votes
6 answers

How can we assume that basic operations on numbers take constant time?

Normally in algorithms we do not care about comparison, addition, or subtraction of numbers -- we assume they run in time $O(1)$. For example, we assume this when we say that comparison-based sorting is $O(n\log n)$, but when numbers are too big to…
75
votes
4 answers

(When) is hash table lookup O(1)?

It is often said that hash table lookup operates in constant time: you compute the hash value, which gives you an index for an array lookup. Yet this ignores collisions; in the worst case, every item happens to land in the same bucket and the lookup…
61
votes
3 answers

Why is binary search faster than ternary search?

Searching an array of $N$ elements using binary search takes, in the worst case $\log_2 N$ iterations because, at each step we trim half of our search space. If, instead, we used 'ternary search', we'd cut away two-thirds of our search space at each…
56
votes
5 answers

How is this sorting algorithm Θ(n³) and not Θ(n²), worst-case?

I just starting taking a course on Data Structures and Algorithms and my teaching assistant gave us the following pseudo-code for sorting an array of integers: void F3() { for (int i = 1; i < n; i++) { if (A[i-1] > A[i]) { swap(i-1, i) i = 0 } } }…
51
votes
4 answers

How do O and Ω relate to worst and best case?

Today we discussed in a lecture a very simple algorithm for finding an element in a sorted array using binary search . We were asked to determine its asymptotic complexity for an array of $n$ elements. My idea was, that it is obvisously $O(\log n)$,…
50
votes
6 answers

How to prove greedy algorithm is correct

I have a greedy algorithm that I suspect might be correct, but I'm not sure. How do I check whether it is correct? What are the techniques to use for proving a greedy algorithm correct? Are there common patterns or techniques? I'm hoping this will…
D.W.
  • 170,893
  • 23
  • 241
  • 528
47
votes
2 answers

Order of growth definition from Reynolds & Tymann

I am reading a book called Principles of Computer Science (2008), by Carl Reynolds and Paul Tymann (published by Schaum's Outlines). The second chapter introduces algorithms with an example of a sequential search which simply iterates through a list…
JW.
  • 581
  • 4
  • 7
40
votes
5 answers

How to come up with the runtime of algorithms?

I've not gone much deep into CS. So, please forgive me if the question is not good or out of scope for this site. I've seen in many sites and books, the big-O notations like $O(n)$ which tell the time taken by an algorithm. I've read a few articles…
38
votes
3 answers

How is algorithm complexity modeled for functional languages?

Algorithm complexity is designed to be independent of lower level details but it is based on an imperative model, e.g. array access and modifying a node in a tree take O(1) time. This is not the case in pure functional languages. The Haskell list…
38
votes
4 answers

How to measure "sortedness"

I'm wondering if there is a standard way of measuring the "sortedness" of an array? Would an array which has the median number of possible inversions be considered maximally unsorted? By that I mean it's basically as far as possible from being…
Robert S. Barnes
  • 2,941
  • 5
  • 26
  • 24
33
votes
2 answers

How asymptotically bad is naive shuffling?

It's well-known that this 'naive' algorithm for shuffling an array by swapping each item with another randomly-chosen one doesn't work correctly: for (i=0..n-1) swap(A[i], A[random(n)]); Specifically, since at each of $n$ iterations, one of $n$…
32
votes
3 answers

Will hardware/implementation affect the time/space complexity of algorithms?

I’m not even a CS student, so this might be a stupid question, but please bear with me... In the pre-computer era, we can only implement an array data structure with something like an array of drawers. Since one have to locate the drawer with…
32
votes
1 answer

Does space complexity analysis usually include output space?

Since most examples of complexity analysis I've seen involve functions that return either nothing (e.g. in-place sort) or a single value (e.g. computation, lookup), I haven't been able to figure this out just by reading examples. If a function…
1
2 3
…
99 100