![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 2
Rep Power: 0
![]() |
I am doing Big-0h. I have some problem with the Big-Oh.
Here is the problem: <font="courier new"> do{ do{ System.out.print("Input characters: "); input = ins.readLine(); }while(input.length()<1); choice = input.charAt(0); switch (choice){ case '1': System.out.println("you choose 1"); break; case '2': System.out.println("you choose to exit"); break; default: System.out.println("You choose others"); } }while(choice !='2'); </font> 1. is the Big-Oh of the outer do while loop "N" or "1"? 2. is the Big-Oh of the inner do while loop "N" or "1"? I have analysed that, the Big-Oh for the outer and inner are O(1), isn't it? Am I in the right way? Thank you |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
How many times does the outer loop run, and how many times does the inner loop run? And no, your answer is not correct.
![]()
__________________
HijackThis Team-SFDC |
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Mar 2005
Posts: 2
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
Your outer loop runs n times, and your inner loop runs n times, so yes n^2... that's what I come up with anyway. Big Oh is sort of funny. I have had two teachers tell me two different answers for the same algorithm.
![]()
__________________
HijackThis Team-SFDC |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|