Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate algorithm's complexity ( infinite algorithms )

Considering this piece of Java code :

import java.util.Scanner;

class BreakWhileLoop {
  public static void main(String[] args) {
    int n;

    Scanner input = new Scanner(System.in);

    while (true) {
      System.out.println("Input an integer");
      n = input.nextInt();

      if (n == 0) {
        break;
      }
      System.out.println("You entered " + n);
    }
  } 
}

Let's take this particular case : the user will always enter any integer except 0.

1.Can i consider this code as an algorithm ?

2.If yes , how to calculate its complexity ?

Thanks

like image 632
MCHAppy Avatar asked Dec 26 '22 02:12

MCHAppy


1 Answers

To avoid trivial answers, let us relax the problem statement by removing the except 0 condition.

Then yes, it is an algorithm, we can call it a 0 acceptor.

Assuming that user input takes constant time, the time complexity is O(N) where N is the length of the nonzero sequence.

like image 99
Yves Daoust Avatar answered Feb 20 '23 15:02

Yves Daoust