0

I am really struggling to calculate the big O notation of this piece of code.

Boolean validInput = false;
while (validInput == false) {
    System.out.println("Please input number of cards");
    if (stdin.hasNextInt()) {
        n = stdin.nextInt();
        if (n > 0) {
            validInput = true;
        } else {
            System.out.println("INVALID INPUT: INPUT MUST BE STRICTLY POSITIVE INTEGER");
        }
    } else {
        System.out.println("INVALID INPUT: INPUT MUST BE STRICTLY POSITIVE INTEGER");
        stdin.next();
    }
}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Shamim
  • 1
  • 1
  • `O(n)` where `n` is the number of wrong inputs. – tkausl Nov 06 '21 at 22:10
  • 1
    @tkausl the asymptotical runtime should be a worst-case analysis based on the input of the algorithm. This algorithm has no input. If the user enters only incorrect inputs or never enters anything, the algorithm does not terminate. – Turing85 Nov 06 '21 at 22:14
  • 2
    I would argue this code has no time complexity. At least no sensible one, you could argue it is constant, you could argue it is linear, it really makes no sense or difference. – luk2302 Nov 06 '21 at 22:18
  • Does this answer your question? [How to find time complexity of an algorithm](https://stackoverflow.com/questions/11032015/how-to-find-time-complexity-of-an-algorithm) – Nowhere Man Nov 06 '21 at 23:45
  • @AlexRudenko I do not think that this is a useful duplicate since it does not mention algorithms that do not satisfy the property of finiteness. – Turing85 Nov 07 '21 at 00:17

1 Answers1

2

Time complexity is a sensible property of an algorithm iff. the algorithms does terminate (Finiteness).

The algorithm shown does not terminate: a user may only enter invalid inputs, or a user may never enter anything at all. It has no time complexity.

Turing85
  • 18,217
  • 7
  • 33
  • 58