Questions tagged [runtime-analysis]

Questions about methods for estimating the increase in runtime of an algorithm as the input size increases.

This question is for analysis of the running time of an algorithm, especially asymptotic running time analysis.

Make sure to refer to our reference questions before asking about the running time of an algorithm:

Many elementary questions about running time can be solved using the techniques in those reference questions, so make sure to check whether your problem can be solved using those standard techniques before asking. Questions asking about elementary aspects of runtime analysis (e.g., undergraduate-level exercises) that don't demonstrate knowledge of these techniques might be closed as a duplicate of the reference question.

1063 questions
197
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,…
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…
63
votes
3 answers

What exactly is polynomial time?

I'm trying to understand algorithm complexity, and a lot of algorithms are classified as polynomial. I couldn't find an exact definition anywhere. I assume it is the complexity that is not exponential. Do linear/constant/quadratic complexities…
60
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…
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]) { …
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…
36
votes
2 answers

Why is the log in the big-O of binary search not base 2?

I am new to understanding computer science algorithms. I understand the process of the binary search, but I am having a slight misunderstanding with its efficiency. In a size of $s = 2^n$ elements, it would take, on average, $n$ steps to find a…
Cyclopropane
  • 463
  • 1
  • 4
  • 7
36
votes
7 answers

Why is selection sort faster than bubble sort?

It is written on Wikipedia that "... selection sort almost always outperforms bubble sort and gnome sort." Can anybody please explain to me why is selection sort considered faster than bubble sort even though both of them have: Worst case time…
RYO
  • 501
  • 1
  • 5
  • 8
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…
23
votes
4 answers

Why does Randomized Quicksort have O(n log n) worst-case runtime cost

Randomized Quick Sort is an extension of Quick Sort in which the pivot element is chosen randomly. What can be the worst case time complexity of this algorithm. According to me, it should be $O(n^2)$, as the worst case happens when randomly chosen…
22
votes
3 answers

Do functions with slower growth than inverse Ackermann appear in runtime bounds?

Some complicated algorithms (union-find) have the nearly-constant inverse Ackermann function that appears in the asymptotic time complexity, and are worst-case time optimal if the nearly constant inverse Ackermann term is ignored. Are there any…
21
votes
2 answers

How to describe algorithms, prove and analyse them?

Before reading The Art of Computer Programming (TAOCP), I have not considered these questions deeply. I would use pseudo code to describe algorithms, understand them and estimate the running time only about orders of growth. The TAOCP thoroughly…
Yai0Phah
  • 621
  • 6
  • 14
20
votes
5 answers

For what kind of data are hash table operations O(1)?

From the answers to (When) is hash table lookup O(1)?, I gather that hash tables have $O(1)$ worst-case behavior, at least amortized, when the data satisfies certain statistical conditions, and there are techniques to help make these conditions…
19
votes
5 answers

How long does the Collatz recursion run?

I have the following Python code. def collatz(n): if n <= 1: return True elif (n%2==0): return collatz(n/2) else: return collatz(3*n+1) What is the running-time of this algorithm? Try: If $T(n)$ denotes the…
1
2 3
70 71