big-o

Best way to do powerOf(int x, int n)?

So given x, and power, n, solve for X^n. There's the easy way that's O(n)... I can get it down to O(n/2), by doing numSquares = n/2; numOnes = n%2; return (numSquares * x * x + numOnes * x); Now there's a O(log(n)) solution, does anyone know how to do it? It can be done recursively. ...

analysis Big Oh notation psuedocode

I'm having trouble getting my head around algorithm analysis. I seem to be okay identifying linear or squared algorithms but am totally lost with nlogn or logn algorithms, these seem to stem mainly from while loops? Heres an example I was looking at: Algorithm Calculate(A,n) Input: Array A of size n t←0 for i←0 to n-1 do if A[i]...

Are there any worse sorting algorithms than Bogosort (a.k.a Monkey Sort)?

My co-workers took me back in time to my University days with a discussion of sorting algorithms this morning. We reminisced about our favorites like StupidSort, and one of us was sure we had seen a sort algorithm that was O(n!). That got me started looking around for the "worst" sorting algorithms I could find. We postulated that a c...

Asymptotic runtime of list-to-tree function

I have a merge function which takes time O(log n) to combine two trees into one, and a listToTree function which converts an initial list of elements to singleton trees and repeatedly calls merge on each successive pair of trees until only one tree remains. Function signatures and relevant implementations are as follows: merge :: Tree ...

How to calculate order (big O) for more complex algorithms (ie quicksort)

I know there are quite a bunch of questions about big O notation, I have already checked Plain english explanation of Big O , Big O, how do you calculate/approximate it?, and Big O Notation Homework--Code Fragment Algorithm Analysis?, to name a few. I know by "intuition" how to calculate it for n, n^2, n! and so, however I am completely...

Algorithm Analysis tool for java

I am looking for an algorithm analysis tool for java that can calculate Big 0 of a function. Ideal I would like to make it part of my build process, along side of my other code metrics tool. Even after searching on google I am unable to find any opensource of commercial tool. Any suggestion would be welcome Thanks ...

How to prove this statement of big o notation?

How to prove this: 4n = O(8n) 8n = O(4n)? So what are the C and n0 values for both cases? ...

How to find the formula of best case and worst case of my algorithm?

I was given a task. Write an algorithm so that, the input of 2 lists of data, will have at least one in common. So, this is my algorithm: (I write the code in php) $arrayA = array('5', '6', '1', '2', '7'); $arrayB = array('9', '2', '1', '8', '3'); $arrayC = array(); foreach($arrayA as $val){ if(in_array($val, $arrayB)){ ar...

What is the actual datastructue used in implementation of collection and their order?

There are different collections are used in Java like hashtable, hashset, vector, treeset, treemap and hashmap. How are they implemented internally? What are the actual datastructure used for these collection? And, What is order? It would be good if we discuss just a little bit about implementation of collection. Thanks, -Abhishek ...

Can someone help with big O notation?

void printScientificNotation(double value, int powerOfTen) { if (value >= 1.0 && value < 10.0) { System.out.println(value + " x 10^" + powerOfTen); } else if (value < 1.0) { printScientificNotation(value * 10, powerOfTen - 1); } else // value >= 10.0 { printScientificNotation(value / 10, powerOfTen + 1); } } assuming...

Landau notation (ide) tools support

It is good idea to have impotant information during developing like Landau notation to know functions's time costs. So it should be documented in sources isn't it? I'm looking for tools that can calculate it. ...

(log(n))^log(n) and n/log(n), which is faster?

f(n)=(log(n))^log(n) g(n)= n/log(n) f=O(g(n))? ...

recurrence solution, too loose a bound?

I have the following worked out: T(n) = T(n - 1) + n = O(n^2) Now when I work this out I find that the bound is very loose. Have I done something wrong or is it just that way? ...

Big Oh Notation - formal definition.

I'm reading a textbook right now for my Java III class. We're reading about Big-Oh and I'm a little confused by its formal definition. Formal Definition: "A function f(n) is of order at most g(n) - that is, f(n) = O(g(n)) - if a positive real number c and positive integer N exist such that f(n) <= c g(n) for all n >= N. That is, c g(n) ...

What is the time complexity of LinkedList.getLast() in Java?

I have a private LinkedList in a Java class & will frequently need to retrieve the last element in the list. The lists need to scale, so I'm trying to decide whether I need to keep a reference to the last element when I make changes (to achieve O(1)) or if the LinkedList class does that already with the getLast() call. What is the big-...

Can hash tables really be O(1)

It seems to be common knowledge that hash tables can achieve O(1) but that has never made sense to me. Can someone please explain it? A. The value is an int smaller than the size of the hash table, so the value is its own hash, so there is no hash table but if there was it would be O(1) and still be inefficient. B. You have to calcu...

Linear complexity and quadratic complexity

I'm just not sure... If you have a code that can be executed in either of the following complexities: A sequence of O(n), like for example: two O(n) in sequence O(n) The preferred version would be the one that can be executed in linear time. Would there be a time such that the sequence of O(n) would be too much and that O(n) would b...

What is the big-O cost function of this algorithm?

How would you characterize the below in big-O notation? rotors = [1,2,3,4,5 ...] widgets = ['a', 'b', 'c', 'd', 'e' ...] assert len(rotors) == len(widgets) for r in rotors: for w in widgets: ... del widgets[0] ...

Big O and Little o

If algorithm A has complexity O(n) and algorithm B has complexity o(n^2), what, if anything, can we say about the relationship between A and B? Note: the complexity of A is expressed using big-Oh, and the complexity of B is expressed using little-Oh. ...

What order of time does the .NET System.String.Length property take?

I had someone advise me to avoid repeatedly calling String.Length, because it was recalculated each time I called it. I had assumed that String.Length ran in O(1) time. Is String.Length more complex than that? ...