0

I am new to coding.Though the following may think irrelevant please help me solve this. I have written the code in two different ways I want to know the complexity and runtime and which will give the faster result with Big-O notation between the two.I know that it can be written using 10 - 20 lines but I want to know the time complexity of the change in the length of the code.

Just a correction, every O(1) algorithm is not equal in execution time just because they are both O(1). Then how do we determine the execution time as they both are O(1)?

CODE1:

package can;

import java.util.Scanner;

public class scan2
{

private static Scanner scanner;
public static void main(String[] args)
 {
    System.out.println
       ("Select an option\n1-apples\n2-bananas\n3-oranges\nFor apples less     than 10 quantity,\neach apple costs 50INR else each apple price is 40INR.\nFor bananas less than 24,\neach banana costs 2INR else 1.5INR.\nFor oranges less than 12,\neach orange costs 5.7INR else 4INR. ");



    int i;
    int j =0;
    float k = 0f;
    scanner = new Scanner(System.in);
    i = (scanner.nextInt());

    if(i<=3&&i>0)
    {
        System.out.println("Please enter the quantity ");
        scanner = new Scanner(System.in);
        j = (scanner.nextInt()); 
    }
    else
    {
        System.out.println("Please choose a valid option ");
    }

if (i == 1&&i>0) 
{       
{
System.out.println("you have entered "+j+" quantites of apples");
}
if (j<10&&j>0)
{   
    k = j*50;
    System.out.println("Total cost is "+k);
}
else if(j>=10&&j>0)
{
    k = j*40;
    System.out.println("Total cost is "+k);
}   
if(j>0)
{   
    System.out.println("Your shopping is completed");
}
else
{
    System.out.println("Please enter valid quantity");
}

}   
if (i == 2)
{
{
System.out.println("you have entered "+j+" quantites of bananas");
}
if (j<24&&j>0)
{   
    k = j*2;
    System.out.println("Total cost is "+k);
}
else if(j>=24&&j>0)
{
    k = j*1.5f;
    System.out.println("Total cost is "+k);
}
if(j>0)
{   
    System.out.println("Your shopping is completed");
}
else
{
    System.out.println("Please enter valid quantity");
}
}   
if (i == 3)
{
{
System.out.println("you have entered "+j+" quantites of oranges");
}
if (j<12&&j>0)
{   
    k = j*5.7f;
    System.out.println("Total cost is "+k);
}
else if(j>=12&&j>0)
{
    k = j*4;
    System.out.println("Total cost is "+k);
}
if(j>0)
{   
    System.out.println("Your shopping is completed");
}
else
{
    System.out.println("Please enter valid quantity");              
}
}


}

}

CODE2:

package can;

import java.util.Scanner;

public class shop 
{


private static Scanner scanner;
public static void main(String[] args) 
{
    System.out.println
           ("Select an option\n1-apples\n2-bananas\n3-oranges\nFor apples      less than 10 quantity,\neach apple costs 50INR else each apple price is 40INR.\nFor bananas less than 24,\neach banana costs 2INR else 1.5INR.\nFor oranges less than 12,\neach orange costs 5.7INR else 4INR. ");



int i;
int j =0;
float k = 0f;
scanner = new Scanner(System.in);
i = (scanner.nextInt());

if(i<=3&&i>0)
{
    System.out.println("Please enter the quantity ");
    scanner = new Scanner(System.in);
    j = (scanner.nextInt()); 
}
else
{
    System.out.println("Please choose a valid option ");
}

if (i == 1&&i>0) 
  {     
    {
    System.out.println("you have entered "+j+" quantites of apples");
    }
    if (j<10&&j>0)
    {   
        k = j*50;
        System.out.println("Total cost is "+k);
    }
    else if(j>=10&&j>0)
    {
        k = j*40;
        System.out.println("Total cost is "+k);
    }   
 }  
 if (i == 2)
   {
     {
       System.out.println("you have entered "+j+" quantites of bananas");
     }
    if (j<24&&j>0)
    {   
        k = j*2;
        System.out.println("Total cost is "+k);
    }
    else if(j>=24&&j>0)
    {
        k = j*1.5f;
        System.out.println("Total cost is "+k);
    }
 }  
 if (i == 3)
  {
    {
        System.out.println("you have entered "+j+" quantites of oranges");
    }
    if (j<12&&j>0)
    {   
        k = j*5.7f;
        System.out.println("Total cost is "+k);
    }
    else if(j>=12&&j>0)
    {
        k = j*4;
        System.out.println("Total cost is "+k);
    }

  }
  if(i<=3&&j>0)
 {  
    System.out.println("Your shopping is completed");
 }
  else if(j<0)
 {
    System.out.println("Please enter valid quantity");
 }
    else if(i<=3&&j == 0)
   {
       System.out.println("Please enter valid quantity");
   }

   }
  }
Kiran
  • 11
  • 3
  • 1
    There is no loop in any of those two. They're O(1). I think you've not really understood what Big-O is about. Big-O allows telling if, when you execute an algorithm on a collection of N elements, if the time will be constant, or will grow linearly, or exponentially, or logarithmically (for example) with the value of N. There is no N and no collection in your code. Big-O isn't relevant. – JB Nizet Jul 04 '16 at 18:34
  • Basic if-else statements are O(1). 1 for-loop makes it O(N). 2 nested for-loops make it O(N^2). There are more complicated ones that make something O(logN) or O(NlogN) that you should look into. – btrballin Jul 04 '16 at 18:35
  • length of code has no direct correlation to execution time as the source is not what is executed. Also every `O(1)` algorithm is not equal in execution time just because they are both `O(1)`. –  Jul 04 '16 at 18:47
  • what you should be concerned with is [cyclomatic complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity) with branching statements. –  Jul 04 '16 at 18:50
  • that's exactly what I am asking every O(1) algorithm is not equal in execution time just because they are both O(1). Then how do we determine the execution time as they both are O(1)? – Kiran Jul 04 '16 at 18:56
  • As I said in my answer, you have to figure out the range of comparisons that can be executed by each code snippet. Find out which inputs give you the min and max number of comparisons to find the execution time. – Chris Gong Jul 04 '16 at 19:00

1 Answers1

1

Each code snippet contains no loops, just if-else statements. Therefore, the runtime of each is O(1), meaning that there is no difference in runtime between the two code snippets. In terms of Big-O, any time that the total number of operations that you are counting is some constant, it always reduces to 1. A similar statement can even be said about a variable number of operations. Suppose the big-O was 3n or 4n^2, in these cases the constant term would be dropped, leaving you with n or n^2. Another note that should be made about big-O is that all terms besides the term with the highest degree are dropped as well. For example, n^2 + 6n + 1 becomes O(n^2). However, your code snippets contain if and else-if statements, which create a certain range of comparisons that can occur during execution. Since this range is constant, the Big-O of each code snippet reduces down to O(1)

Chris Gong
  • 8,031
  • 4
  • 30
  • 51