Computational Complexity (Big O Notation)
Nov 5, 2014
I've been trying to learn more about Big O Notation and I've gotten stuck on a few pieces of code. What is the computational complexity for the following pieces of code?
1:
for(int i = n; i > 0; i /= 2) {
for(int j = 1; j < n; j *= 2) {
for(int k = 0; k < n; k += 2) {
// constant number of operations
[Code] .....
5 : Determine the average processing time of the recursive algorithm. (int n) spends one time unit to return a random integer value uniformly distributed in the range [0,n] whereas all other instructions spend a negligibly small time(e.g., T(0) = 0)
int myTest(int n) {
if(n <= 0) return 0;
else {
int i = random(n - 1);
return myTest(i) + myTest(n - 1 - i);
6 : Assume array a contains n values, the method randomValue takes a constant number c of computational steps to produce each output value, and that the method goodSort takes n log n computational steps to sort the array.
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++)
a[j] = randomValue(i);
goodSort(a);
}
View Replies
ADVERTISEMENT
Aug 12, 2014
So recently I began Data Structures as core subject and the tutorials in this forum are great.
Right now, I seem to have trouble with Big Oh Notation algorithm and what is the mathematical side to it. "f(n) <= c.g(n), for n>=0.
The question I am working on is: Suppose we are maintaining a collection C of elements such that, each time we add a new element to the collection, we copy the contents of C into a new array list of just the right size. What is the running time of adding n elements to an initially empty collection C in this case?
View Replies
View Related
Feb 1, 2015
I need to figure out how to calculate the remove method complexity in the worst best and average case same as the insert method
public void insert(String Word)
{
size++;
int pos = myhash(Word);
WordNode nptr = new WordNode(Word,null);
if (table[pos] == null)
table[pos] = nptr;
[Code] .....
View Replies
View Related
Mar 16, 2014
Suppose i have written a method like this
public void myWork(){
mergeSortMyData(); O(nlgn)
....
someProcessing(); O(n^2)
someprocessing2(); O(n^2)
}
then what will be time complexity of my code...according to me it will be O(nlgn + n^2) ...is it correct ?
View Replies
View Related
Sep 9, 2014
I can't able to find the theta for some type of code like.
for(i=1;i<=n;i++){
for(j=i;j>=1;j=j/3){
....
}
}
How to find the theta for the above code.
for(i=1;i<=n;i++){
for(j=i;j>=1;j=j/K){
....
}
}
and How to find number of steps or time complexity for inner loop if k=3 (odd)
View Replies
View Related
Feb 13, 2015
with arrays its binary search which finds a value in O(Logn) time but what about linked lists ? the most effiecient algorithm will be O(n) ? and i know that binary search cannot be implement on a linked list , therefore , the only way to search a linked list is a linear search ?
View Replies
View Related
Mar 6, 2015
What is Big O notation in java, what all things we need to remember before choosing a collection framework's class.
What each terms in big O notation means, Is there any blog or site that explain big O for java.
View Replies
View Related
Apr 12, 2014
Compute the total number of operations for each of the following algorithm and define the value of Big Oh in its worst-case.
i.int jum = 0;
for (int x = 1; x<= n; x++) {
for (int y = 1 ; y <= 10; y++) {
for (int k = 0; k<= n*n; k++) {
jum = jum * x + y;
[Code] .....
What it actually means by compute the total number of operation ? My answer is
(i) 2 assignment , 1 addition and 1 multiplication
(ii) 4 assignment , 1 multiplication , 2 addition
The value of Big O is
(i) 10n^3 , so the answer is n^3
(ii) n^4+1 , so the answer is n^4
May i know is my answer is correct ??
View Replies
View Related
Apr 14, 2014
I've to create a program to get user input in scientific notation. Now i'm confused that how can i check that how user have used the scientific notation. For example there are many ways to write a number in scientific notation. Also a user can either write a number as:
6.7E2 OR 6.7*10^2
How to handle this input and further convert it to double?
View Replies
View Related
May 20, 2014
I'm having trouble figuring out the Big-O notation for this loop. For now I only have it in pseudocode. I hope you understand it anyway
for i = 1 to n do
j = 1
while j <= n/2 do
print "*"
j = j + 1
end while
end for
View Replies
View Related
May 23, 2015
Java Code:
public class MountainBike extends Bicycle {
// the MountainBike subclass has
// one field
public int seatHeight;
// the MountainBike subclass has
// one constructor
public MountainBike(int startHeight, int startCadence,
[Code] ....
At first,
Java Code: public int seatHeight; mh_sh_highlight_all('java');
tells us that seatHeight is NOT a static field (because of the absence of static keyword).
Whereas in the constructor, the absence of dot notation (like something like this.seatHeight) in
Java Code: seatHeight = newValue; mh_sh_highlight_all('java');
shows that it IS a non-member/static variable.
How does this make sense?
View Replies
View Related
Oct 24, 2014
My question is to evaluate a Postfix notation entered from keyboard. I have no errors in my code but it prints only :
Exception in thread "main"
java.util.NoSuchElementException
at ArrayStack.pop(PostFixEvaluation.java:72)
at PostFixEvaluation.evaluatePostfix(PostFixEvaluatio n.java:107)
at PostFixEvaluation.main(PostFixEvaluation.java:140)
I tried many values but it prints the same exception all the time.
Here is my code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
interface Stack<E> {
[Code] ....
View Replies
View Related
Apr 7, 2014
So I am supposed to be changing infix notation to postfix notation using stacks. This is simply taking a string "3 + 5 * 6" (infix) and turning it into (3 5 6 * +" (postfix).
To do this, we are to scan the string from left to right and when we encounter a number, we just add it to the final string, but when we encounter an operand, we throw it on the stack. Then if the next operand has a higher input precedence than the stack precedence of the operator on the top of the stack, we add that operator to the stack too, otherwise we pop from the stack THEN add the new operator.
I am supposed to be utilizing a hash map but I don't see how you would go about doing this. We are supposed to store operators on the hash map but operators need their own character, input precedence, stack precedence, and rank. How do you use a hash map when you need to tie a character to 3 values instead of just 1? I just don't get it.
The following is our Operator class that we are to use. Another problem is this isn't really supposed to be modified, yet we were given two important variables (inputPrecedence and outputPrecedence) that we can't have nothing to be initialized to and no way of accessing? So that might be where a hash map comes in but I am not sure. I am not very sure on how they exactly work anyway...
public class Operator implements Comparable<Operator>
{
public char operator; // operator
privateint inputPrecedence; // input precedence of operator in the range [0, 5]
privateint stackPrecedence; // stack precedence of operator in the range [-1, 3]
[Code] ....
So my question mostly revolves around how I tie an Operator character to its required values, so I can use it in my code to test two operators precedence values.
My original thought was turn string into character array, but then I would need nested for/while loops to check if it is a number or letter, or if it is an operator and thus result in O(n^2) time
View Replies
View Related