Can anyone help me figure out what this algorithm does? [closed] - algorithm

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have this algorithm here:
sumup(int n) {
int s = ???, k = 0;
while(k != n) {
k = s*(2*k-1)*(2*k-1);
s = k;
}
return s;
}
And I need to find out what its purpose is. It doesn't even seem to work with most numbers and it just returns n again anyway, once its done.
Does anybody have any idea what this algorithm is used for?
I assumed it was for square roots, but it doesn't really seem to work either way.

At the end of the for loop s and k are equal. Before the next iteration k != n is checked. This is equivalent to s != n. So the loop runs until s == n holds and then n is returned. So the function get the input n, runs for some time and returns n at the end.
The questions are:
Does it terminate? Under what conditions?
Only if s and n fit together. E.g. if 0 < n < s holds the algorithm will not terminate.
How long does it take, if it terminates?
k is initialized with 0 and becomes the value of s after the first iteration. From there it is basically cubed every iteration. Solving s^(3^x) = n leads to a complexity of Θ(log log n).

Related

Optimizing a sorting algorithm by replacing the one of the recursion with a another function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Currently I have this sorting algorithm:
public static void sort(int[] A, int i, int j) {
if (i == j) return;
if(j == i+1) {
if(A[i] > A[j]) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
else {
int k = (int) Math.floor((j-i+1)/3);
sort(A,i, j-k);
sort(A,i+k, j);
sort(A,i,j-k);
}
}
It's sorting correctly, however, the asymptotic comparison is quite high: with T(n) = 3T(n-f(floor(n/3)) and f(n) = theta(n^(log(3/2)3)
Therefore, I'm currently thinking of replacing the third recursion sort(A,i,j-k) with an newly written, iterative method to optimize the algorithm. However, I'm not really sure how to approach the problem and would love to gather some ideas. Thank you!
If I understand this correct, you first sort the first 2/3 of the list, then the last 2/3, then sort the first 2/3 again. This actually works, since any misplaced items (items in the first or last 2/3 that actually belong into the last or first 1/3) will be shifted and then correctly sorted in the next pass of the algorithm.
There are certainly two points that can be optimized:
in the last step, the first 1/3 and the second 1/3 (and thus the first and second half of the region to sort) are already in sorted order, so instead of doing a full sort, you could just use a merge-algorithm, which should run in O(n)
instead of sorting the first and last 2/3, and then merging the elements from the overlap into the first 1/3, as explained above, you could sort the first 1/2 and last 1/2 and then merge those parts, without overlap; the total length of array processed is the same (2/3 + 2/3 + 2/3 vs. 1/2 + 1/2 + 2/2), but the merging-part will be faster than the sorting-part
However, after the second "optimization" you will in fact have more or less re-invented Merge Sort.

How can I find Kth element in a m*n matrix that each rows(m) are in order with O(m(log m +log n)) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
suppose that we have a m*n matrix that each rows are in order. so, i only know that order of best algorithm for this problem is O(m(log m + log n)).
(It was a test question and result is this order)
but i don't know how this algorithm works
One idea can be like this.
If I ask you what is the rank of a given number x in the original matrix? How do you answer this question?
One answer can be:
Just binary search the first occurrence of x or greater element on each row. and then add the individual ranks.
int rank = 1;
for (int i = 0; i < m; ++i) {
rank += std::lower_bound(matrix[i].begin(), matrix[i].end(), x);
}
This can be done in O(m * log n) time(m binary searches on n sized arrays).
Now we just need to do a binary search on x(between 0 and INT_MAX or matrix[0][k]) to find the kth rank. Since INT_MAX is const, that will make the overall time complexity O(m * log n) theoretically. One optimization, which can be done use intelligent ranges in place of matrix[i].begin(), matrix[i].end().
PS: Still wondering the O(m*(log m + log n)) or O( m * (log mn)) solution.

coverting arraylist to array in less than O(n) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am currently working on program and want to convert ArrayList to an array but in less than O(n) time.
for (int i = 0; i < list.size(); i++) {
if (list.get(i) != null) {
arr[i] = list.get(i);
}
}
If n is the length of the list then O(n) means in this case that you look at each element in the list and copy it.
Now you say you want to convert it in less than O(n). This means you have to ignore some elements in the list. if not it would be O(n) again. But which do you ignore? Remember you are not allowed to look at all elements else it would be in O(n) again.
Let's say you know that the list contains booleans where n/2 are true and the others are false. In the best-case scenario all true values would be in the first half of the list.
Now you can stop iterating at n/2 of the list but you need to add the false values again to your Array. Now you are in O(n) again.
Let's make another assumption. You can always ignore the last value of the list. This means that you only iterate n-1 times that then is O(n-1) but in big O notation, you ignore constants so it gets O(n) again.
It is not possible to copy all n elements of a list to an Array in lower than O(n).

O(n) or O(n)^2 time complexity of the algorithm? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
What is the time complexity of the following algorithm?
int j = 0;
while (j<n) {
for (int i = 0; i < n; i++) {
x++;
j++;
}
}
I tried it and calculated it like this: n*n = n^2, so result would be O(n)^2.
But I have second thoughts that it could also be like this: n+n= 2n. Result: O(n).
I know that if you have two for loops, you should multiply your n's. But here we have while and for, so I don't know.
it is actually just O(n).. (not O(n+n), not O(2n), not O(n^n), just O(n) )
because since you increment j up to n in the inner loop
as soon as you finish the inner loop of n elements, the outer condition will be false.. so it will exit, after n iterations
I think your understanding of determining time complexity with programs involving loops is a bit off. The general approach is to count the number of iterations. The complexity of the generic program:
Loop until SomeCondition:
DoStuff()
Is O([#iterations]*[complexity of DoStuff]). So if the number of iterations is proportional to some variable n and DoStuff is proportional to some variable m, then this program is O(n*m).
Circling back to your question: we see that the inner for loop is proportional to the variable n. Now we ask ourselves "How many iterations does it take for the while loop to reach its condition?". Well that depends on how much j grows each iteration! As pointed out by CaldasGM j increases by one for every iteration of the inner for loop. This means that j grows by n each iteration and so the while loop will exit after one iteration!
So the complexity of this program is O([#iterations]*[Complexity of loop contents]) = O([1*n]*[1]) = O(n).
Hope this helps your understanding :)

Go Fibonacci sequence generator [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
This is my fibonacci generator:
package main
import "fmt"
func main() {
for i, j := 0, 1; j < 100; i, j = i+j,i {
fmt.Println(i)
}
}
It's working, but I don't know how can I improve it, I'd like more expert approaches about it, Thanks...
I assume you are talking about improving the time complexity (and not the code complexity).
Your solution computes the Fibonacci numbers in O(n) time. Interestingly, there exists an O(log n) solution as well.
The algorithm is simple enough: Find the nth power of matrix A using a Divide and Conquer approach and report (0,0)th element, where
A = |1 1 |
|1 0 |
The recursion being
A^n = A^(n/2) * A^(n/2)
Time complexity:
T(n) = T(n/2) + O(1) = O(logn)
If you think about it with a piece of paper, you'd find that the proof is simple and is based upon the principle of induction.
If you still need help, refer to this link
NOTE: Of course, the O(logn) time is true only if you want to find the nth Fibonacci number. If, however, you intend to print ALL of the n fib numbers, theoretically, you can not have a better time complexity than you already have.

Resources