Im quite new to computational complexity, but i know that a nested for loop will give O(n^2). in my case i have a for loop that calls a function which has a for loop within it. will the complexity be O(n) or worse?
public static void main(String[] args) {
for(int i = 0; i < 10; i++){
if(i != 0){
System.out.println();
printt(i);
}
}
}
public static void printt(int i){
for(int j = 0; j <= 10; j++ ){
if(j !=0 ){
System.out.print(j*i+" ");
}
}
}
}
Think of the number of print statements(the second one itc) that are executed if you run this snippet of code.
The easiest way to reason about this is to go ahead and run the program, and you will notice that you have 81 values being printed out, which tells you that you have 9 calls to the nested function for each run of the outer loop (9 time again). So it ends up being O(n^2).
Related
Can anyone find me the big O notation of the following code?
sum=0;
for(i=0;i<n;i++)
{
for(int j=0;j<i*i;j++)
{
for(int k=0;k<j;k++)
sum++;
}
}
What is the complexity of this code?
public class test5{
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
for (int i = 1; i<=n; i++) {
for (int j = 1; j<=i; j++) {
System.out.print ("*");
}
System.out.println();
}
for (int i = n; i>=1; i--) {
for (int j = 1; j<=i; j++) {
System.out.print ("*");
}
System.out.println();
}
}
}
My assumption is that it will take O(n^2) operations because n*(n/2) + n*(n/2).
Am I right?
You are correct, a tight upper asymptotic bound for both the first and second nested loop blocks—say T_A(n) and T_B(n), respectively—is O(n^2), and hence the function as a whole runs as O(n^2), asymptotically.
You can analyze this in detail using Sigma notation to count the number of basic operations in the inner loop blocks for each of the nested loop blocks T_A(n) and T_B(n):
Where we've treated the System.out.print ("*"); operation as basic operation.
I have a question considering Big O runtime and the indexOf method within LinkedList, and ArrayList. How can I come up with a Big O runtime assumption and how would it be different in a Linked List as opposed to an Array List?
LinkedList indexOf()
public int indexOf(Object value)
{
int results = -1;
boolean done = false;
Node<E> ref = head.next;
for(int i = 0; i < size && !done; i++)
{
if(value.equals(ref.value))
{
results = i;
done = true;
}
ref = ref.next;
}
return results;
}
ArrayList indexOf()
if (o == null) {
for (int i = 0; i < size; i++)
if (Values[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(Values[i]))
return i;
}
return -1;
I apologize if this is a trivial question to some but I am going to need to understand how to come up with a Big O runtime of a method.
In both these implementations you have nothing better to do than go over the list one element at a time and compare it to the value you're looking for. At the worst case, you'd be going through the entire list, giving a complexity of O(n).
I just want to know the break down for the Big O execution growth rate for this code, I have try to calculate it but it I got the for loops wrong. so I am completely stuck on this now.
void doInter(int setA[], int setB[], int sizeA, int sizeB)
{
const int MAX = 10;
int sizeR;
int results [MAX];
// validate sizeA and sizeB
if ((sizeA == 0) || (sizeB == 0))
{
cout << "one of the sets is empty\n";
}
// save elements common to both sets
for (int i = sizeR = 0; i < sizeA; i++ )
{
if (member(setB, setA[i],sizeB))
{
results[sizeR++] = setA[i];
}
}
{
cout << results[i] << " ";
}
cout << "}" << endl;
}
bool member (int set[], int n, int size)
{
for (; size > 0; --size)
{
if (set[size-1] == n)
{
return true;
}
}
return false;
}
The complexity of this code is O(sizeA * sizeB). It is relatively easy to compute - first compute the complexity of the inner function member - this is a single cycle and in the worst case it will perform sizeB iterations. Now in the outer function you call this function in a cycle of size sizeA. Thus the overall complexity is the two complexities multiplied. The remaining operations are relatively simple with regards to this two cycles.
Also an example where this complexity is achieved is easy to see - use two arrays with no common elements.
This method needs to sort and array and I am close with the following code but what it does is puts the list forwards to backwards i.e. if the list was "5, 4, 3" this would change it to "3, 4, 5". This is because after the if statement completes it goes back to the for and once it is finished the for it sets lowest back to 1000. How do I make it not set it back to 1000 each time?
public void sortList()
{
for(int i=0; i<myList.size(); i++)
{
int lowest = 1000;
if(myList.get(i)<lowest)
{
myList.add(0, myList.get(i));
myList.remove(i+1);
}
}
}
This :
public void sortList() {
int lowest = 1000;
for(int i=0; i<myList.size(); i++)
{
if(myList.get(i)<lowest)
{
myList.add(0, myList.get(i));
myList.remove(i+1);
}
}
}
You simply declare lowest outside of the loop, and update it in the loop.
With the example you gave, I would move the 'lowest' declaration out of the for loop, like so:
int lowest = 1000;
for(int i=0; i<myList.size(); i++)
{
if(myList.get(i)<lowest)
{
myList.add(0, myList.get(i));
myList.remove(i+1);
}
}
That way it's not reset to 1000 with each for loop. You can then update it within the loop as needed as you would any other numeric variable.