Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

Questions tagged [algorithm-analysis]

Analyzing an algorithm to determine its time and space performance.

Filter by
Sorted by
Tagged with
34 votes
5 answers
16k views

In divide and conquer algorithms such as quicksort and mergesort, the input is usually (at least in introductory texts) split in two, and the two smaller data sets are then dealt with recursively. It ...
beta's user avatar
  • 1,012
32 votes
5 answers
49k views

I have the follow algorithm which finds duplicates and removes them: public static int numDuplicatesB(int[] arr) { Sort.mergesort(arr); int numDups = 0; for (int i = 1; i < arr.length; ...
chopper draw lion4's user avatar
23 votes
4 answers
4k views

What term can I use to describe something with O(N log N) complexity? For example: O(1): Constant O(log N): Logarithmic O(N): Linear O(N log N): ?????? O(N2): Quadratic O(N3): Cubic
matiascelasco's user avatar
21 votes
6 answers
5k views

I recently finished a course on advanced algorithms, and another on complexity & computability theory, and in the past few days my mind has been somewhat preoccupied by this question. Why don't we ...
cliesens's user avatar
  • 345
20 votes
8 answers
28k views

I have always heard that linear search is a naive approach and binary search is better than it in performance due to better asymptotic complexity. But I never understood why is it better than linear ...
Aseem Bansal's user avatar
  • 3,044
15 votes
2 answers
9k views

I am solving an algorithm question and my analysis is that it would run on O(2^sqrt(n)). How big is that? Does it equate to O(2^n)? Is it still non-polynomial time?
Gaara's user avatar
  • 261
13 votes
7 answers
8k views

I am a programmer and have just started reading Algorithms. I am not completely convinced with the notations namely Bog Oh, Big Omega and Big Theta. The reason is by definition of Big Oh, it states ...
Pradeep's user avatar
  • 313
13 votes
3 answers
1k views

I was going through the analysis of quicksort in Sedgewick's Algorithms book. He creates the following recurrence relation for number of compares in quicksort while sorting an array of N distinct ...
damon's user avatar
  • 299
9 votes
9 answers
3k views

Using Big O notation, it is clear that I should go with the more efficient approach, but I know that there is a significant cost in terms of initialization for more efficient solutions. The Problem: ...
BlueyRules's user avatar
8 votes
5 answers
18k views

Still on my quest to compress/decompress files with a Java implementation of Huffman's coding (http://en.wikipedia.org/wiki/Huffman_coding) for a school assignment. From the Wikipedia page, I quote: ...
Saturn's user avatar
  • 3,937
8 votes
1 answer
2k views

I recently implemented the Damerau-Levenshtein distance algorithm from the pseudocode on Wikipedia. I couldn't find any explanation of exactly how it works and the pseudocode uses completely ...
James Jensen's user avatar
7 votes
3 answers
15k views

We know that binary search takes O(log n) in Big O notation but if we need to run twice an algorithm of O(log n), would it be the same as O(n) in time complexity? For example, if I have a method to ...
Walter R's user avatar
  • 181
7 votes
2 answers
4k views

We read on Wikipedia > Iterative deepening depth-first search that The space complexity of IDDFS is O(bd), where b is the branching factor and d is the depth of shallowest goal. Wikipedia also gives ...
Dan Burton's user avatar
6 votes
8 answers
3k views

Programmers often talk about the time complexity of an algorithm, e.g. O(log n) or O(n^2). Time complexity classifications are made as the input size goes to infinity, but ironically infinite input ...
James C's user avatar
  • 191
6 votes
3 answers
4k views

I have written a data cleansing application which, for the most part, works well. It is not designed to handle large volumes of data: nothing more than about half a million rows. So early on in the ...
Bob Tway's user avatar
  • 3,636
6 votes
5 answers
2k views

I have a sparse matrix that contains several islands of unknown size. I would like to find the highest peak of each islands. Consider this matrix as an example: 0 0 1 0 0 0 0 0 0 1 2 1 0 0 0 0 0 3 2 ...
MrTJ's user avatar
  • 161
6 votes
2 answers
7k views

I was going through the Introduction to Algorithms by Cormen et al.In the chapter titled Amortized Analysis,the difference between accounting and potential methods is given like this The accounting ...
damon's user avatar
  • 299
5 votes
3 answers
6k views

I had an exam today and I feel that I did pretty well, except I could not for the life of me figure out what appears to be an unbelievably simple question. We were asked to give theta notation run ...
kang's user avatar
  • 61
5 votes
1 answer
10k views

The A* Algorithm is the optimal (provided the heuristic function is underestimated), complete & admissible (provided some conditions). I know the proofs of admissibility & optimality. But how ...
vintesh's user avatar
  • 261
5 votes
4 answers
684 views

I have an n x n matrix which I need to convert into a list sorted by value. Starting with the maximum value cell at (row x1, col y1), I must immediately exclude all cells where (x >= x1, y <= y1)...
Irfan434's user avatar
  • 187
5 votes
2 answers
1k views

I have set of spatial points defined by coordinates (x,y) . I want to find the bounding rectangle of given area that maximizes the number of point inside the rectangle. The obtained rectangle should ...
sanket's user avatar
  • 59
5 votes
2 answers
397 views

I have a problem I am developing a solution for and currently I solve it with a brute force solution that checks all possibilities. It works for small numbers of bins but I'd like to work with a ...
CodeMonkey42's user avatar
4 votes
2 answers
303 views

I've been assigned to explore implementing (along with modifications, so understanding it is a must) this algorithm for the 'Redistricting Problem': https://dl.acm.org/doi/pdf/10.1145/3274895.3274979 ....
Samia Zaman's user avatar
4 votes
3 answers
319 views

Consider an^2 + bn + c. I understand that for large n, bn and c become insignificant. I also understand that for large n, the differences between 2n^2 and n^2 are pretty insignificant compared to the ...
Adam Zerner's user avatar
4 votes
1 answer
2k views

I am reviewing basic algorithms from a book called Algorithms by Robert Sedgewick, and I came across a problem in MergeSort that I am, sad to say, having difficulty solving. The problem is below: ...
hulkmeister's user avatar
4 votes
1 answer
1k views

Mostly in all routers and operating systems the Longest Prefix Match algorithm is used for searching the trie of IPv4 addresses. Packet filters like Iptables don't need some special data structure for ...
red0ct's user avatar
  • 99
4 votes
1 answer
219 views

I have following piece of code: int sum = 0; for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) for (int k = 1; k <= N; k = k*2) for (int h = 1; h <= k; h++)...
Jevgeni Smirnov's user avatar
4 votes
1 answer
169 views

Suppose you want to color the vertices of a graph in a greedy fashion, given a predetermined order of these vertices. The goal is to avoid giving two adjacent (linked by an edge) vertices the same ...
Kuifje's user avatar
  • 151
3 votes
6 answers
888 views

So I was recently given a coding assignment from a large financial firm, and I thought of two ways to solve the problem. One of the ways involved 1 outer for loop and 1 inner for loop. In this case, ...
Tyler M's user avatar
  • 89
3 votes
1 answer
436 views

There was a question asking what the "O" stands for in "Big O", the answer to which seems to be "Ordnung von"/"order of". I wonder what's the origin of the ...
xji's user avatar
  • 791
3 votes
2 answers
31k views

For instance if i have an algorithm that is O(n2) and it will run for 10 seconds for a problem size of 1000. Now if i were to double the problem size to 2000 i want to know the approximate run time in ...
user2733436's user avatar
3 votes
1 answer
320 views

I'm doing a running time analysis using the aggregate method and I'm a bit confused about what would be the result in this case. I have some intuition in inferring that it would be O(mlog(n)) but I'm ...
neo's user avatar
  • 51
3 votes
3 answers
6k views

I was thinking about inefficient algorithms based on randomness and wondered how to categorise them. For instance. Say you wanted to generate all the numbers from 1 to N in a random order but only ...
Fogmeister's user avatar
3 votes
2 answers
10k views

I am learning about analysis of algorithms. I came across the term "upper bound" and "lower bound" in "worst-case" running time of an algorithm. Are they applicable to only the "worst case" or can ...
user3902660's user avatar
3 votes
4 answers
8k views

I have an exercise for my algorithms and data structures class, where I basically have to implement a divide and conquer algorithm or function called check_distance to determine whether all numbers in ...
user avatar
3 votes
2 answers
1k views

I understand how to get a general picture of the big O of a nested loop, but what would be the operations for each loop in a nested for loop? If we have: for(int i=0; i<n; i++) { for(int j=i+...
ohbrobig's user avatar
  • 139
3 votes
1 answer
665 views

I'm trying to put together an algorithm that will display the node degree for every node in a breadth first tree graph (assume BFS was called). Assume it's an undirected graph. I'm not sure how to ...
stackuser's user avatar
  • 185
3 votes
1 answer
553 views

I want to sole this problem: Write a method to return all valid combinations of n-pairs of parentheses. The method should return an ArrayList of strings, in which each string represents a ...
user72708's user avatar
  • 159
3 votes
3 answers
3k views

Is there anything that can calculate the Big-O notation of a function? While learning about Big-O, it is implied that it is pretty trivial to do in your head once you know the basic rules, however if ...
Anon's user avatar
  • 3,649
3 votes
0 answers
235 views

Given the problem... Given a String comprising of non-alhpabetical symbols, and a Lookup Table where a subset of those symbols may equate to a single alphabetical character, output all possible ...
Kevin's user avatar
  • 131
2 votes
7 answers
4k views

An asymptote in mathematics represents a value (line) to which the observed function constantly approaches. In other words, as the value of X increases towards infinity, i.e. decreases towards minus ...
dassd's user avatar
  • 147
2 votes
4 answers
3k views

Is it still relevant? Instead of var result = new List<int>(); for (var i = 0; i < prev.Count; ++i) { result.Add(prev[i] * 2); } where the result.Add, prev[i], and * 2 instructions are ...
Josh Grosso's user avatar
2 votes
7 answers
580 views

An interviewer asked me this question: Given a function f(ar[]) where ar[] is an array of integers, this functions claims to sort ar[] and returns it's sorted version ars[]. Determine if this ...
Sourabh Khandelwal's user avatar
2 votes
4 answers
1k views

For completely random reasons*, I wrote some code that calculates and displays the following expression: P=1*(2+2)(3+3+3)(4+4+4+4)..(n+n...… Which is equivalent to (n!)^2 The version I wrote (...
user avatar
2 votes
4 answers
16k views

I need to find the time complexity in terms of Big Oh notation for the following program which computes the factorial of a given number: The program goes like this: public int fact(int n){ if (n &...
Pradeep's user avatar
  • 313
2 votes
1 answer
25k views

I found this nested loop to calculate the Big-O notation. for(i=0;i<n;i++) for(j=0;j<i*i;j++) for(k=0;k<j;k++) I got the time complexity for the algorithm with this polynomial ...
malintha's user avatar
2 votes
3 answers
2k views

This is a very simple function to find if a string is composed of unique characters. I believe that this is a O(n) time complexity as it loops once and has a single if condition. Am I correct? Is ...
Pradeepl's user avatar
  • 133
2 votes
3 answers
443 views

I think that I am trying to solve a problem somewhat similar to a knapsack problem. I am not sure though. Please see the input given below and my solution. There are three types of items and 4 ...
samarasa's user avatar
  • 123
2 votes
1 answer
595 views

I am currently doing some exercises with algorithms and I find myself in a huge problem. It seems that everybody understands this type of problem but I have a really hard time figuring it out. For ...
Samed Škulj's user avatar
2 votes
1 answer
1k views

I have this question which I need answered: What is the asymptotic running time for the following piece of code? if (N < 1000) for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) ...
owwyess's user avatar
  • 145