Calculate the complexity of an algorithm (infinite algorithms)

Given 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);
    }
  } 
}

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?

thank

+4
source share
4 answers

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

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

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

+6
source

. , . , , .

+2

- , , , .

You can follow the details and evidence of this in this famous Wegner article , entitled "Why is the interaction more powerful than the algorithm"?

0
source

All Articles