LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2011, 11:48 AM   #1
mscoder
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Rep: Reputation: Disabled
Question help needed in understanding the complexity in case of if-else,while within for loop.


Code:
for(i=0;i<n;i++)
{
while(..some conditions...){}
}
}
Code:
for(i=0;i<n;i++)
{
if(...){}
else{}
}
Code:
if(....)
{
if(..){}
}
Please help me have a clear idea as what would be the time complexities of such code segments in my code..or please share a link for the same..thanx in advance for your help..

Last edited by mscoder; 09-23-2011 at 12:32 PM.
 
Old 09-23-2011, 12:09 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
I would guess (and I may be wrong) that in first example, the complexity would be n times the complexity of the inner loop as long as the number of iterations in the inner loop does not depend on i.
In the second example, the complexity should be n since the body of if statement is executed at most once.
The third example does not contain any loops, so the time required to execute it will be some constant value.
 
1 members found this post helpful.
  


Reply

Tags
compare



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Understanding the while Loop LXer Syndicated Linux News 0 04-08-2011 01:00 AM
LXer: Understanding the for Loop LXer Syndicated Linux News 0 04-05-2011 03:40 AM
Simple BASH script; understanding loops and case Sinensis Linux - Newbie 1 06-17-2010 04:32 AM
bash script to test string complexity (like password complexity) robertjinx Linux - Server 2 05-12-2010 02:58 PM
Piping into a Case Statement embedded in a while loop telecom_is_me Programming 5 07-02-2008 04:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration