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++;
}
}
Related
How we can decrees time complexity of finding subarray of a given array in O(n) from O(n^3)
void subArray(int arr[],int n)
{
int i=0,j,k;
while(i<n)
{
j=i;
while(j<n)
{
k=i;
while(k<=j)
{
cout<<arr[k]<<" ";
k++;
}
cout<<endl;
j++;
}
i++;
}
}
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).
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).
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.