Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 2nd, 2005, 12:32 AM   #1
mythinky
Newbie
 
Join Date: Mar 2005
Posts: 2
Rep Power: 0 mythinky is on a distinguished road
Question Big Oh Problem

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
mythinky is offline   Reply With Quote
Old Mar 2nd, 2005, 8:22 AM   #2
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 groovicus is on a distinguished road
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
groovicus is offline   Reply With Quote
Old Mar 2nd, 2005, 8:33 AM   #3
mythinky
Newbie
 
Join Date: Mar 2005
Posts: 2
Rep Power: 0 mythinky is on a distinguished road
Quote:
Originally Posted by groovicus
How many times does the outer loop run, and how many times does the inner loop run? And no, your answer is not correct.
So u mean O(n^2)???
mythinky is offline   Reply With Quote
Old Mar 2nd, 2005, 8:42 AM   #4
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 groovicus is on a distinguished road
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
groovicus is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:47 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC