LinuxQuestions.org
Review your favorite Linux distribution.
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-08-2012, 08:48 PM   #1
nguyeng
LQ Newbie
 
Registered: Jan 2011
Distribution: Slackware
Posts: 18

Rep: Reputation: 1
Is this right? Finding big theta


Hello all, I'm doing some studying for an exam that I will have on Monday and was going over some practice problems. I'm not sure if I was able to grasp the concept of asymptotic analysis well enough. I would just like to check my answer and explain my thought process behind getting the answers to see if I'm doing these correctly. I would ask my professor but it is the weekend and the exam is first thing in the morning on Monday. Thanks for the help.

Determine big theta for the following code fragments in the average case. Assume
that all variables are of type int.

(h) Assume array A contains a random permutation of the values from 0 to
n - 1.

Code:
sum = 0;
for (i=0; i<n; i++)
for (j=0; A[j]!=i; j++)
sum++;
Outer loop will be executed n times. Inner loop is executed n/2 times on average. So, bigTheta(n(n/2)) simplified to bigTheta(n^2).

(i)
Code:
sum = 0;
if (EVEN(n))
for (i=0; i<n; i++)
sum++;
else
sum = sum + n;
If n is even then n executions, else 1. Seeing that n will be even half of the time. The average should be both clauses averaged which is (n+1)/2, which is bigTheta(n).

Last edited by nguyeng; 09-08-2012 at 08:51 PM. Reason: Anyone know how to make the code boxes longer? (No scroll bar)
 
Old 09-09-2012, 06:26 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Looks good to me.


PS it seems like using [INDENT] tags inside [CODE] boxes makes the [CODE] boxes tiny, you can just use whitespace to indent inside [CODE] boxes:

Code:
sum = 0;
if (EVEN(n))
   for (i=0; i<n; i++)
      sum++;
else
   sum = sum + n;
 
Old 09-09-2012, 08:10 AM   #3
nguyeng
LQ Newbie
 
Registered: Jan 2011
Distribution: Slackware
Posts: 18

Original Poster
Rep: Reputation: 1
Cool. Thanks for the assistance!
 
  


Reply



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
how to use Big O, Big Omega, and Big Theta to find running time of algorithms naveed17 Programming 3 03-17-2011 10:25 AM
difference between big theta, big omega & big o mahboob ul haq Linux - Newbie 1 01-29-2011 01:36 PM
Big O, Big Omega, and Big Theta oulevon Programming 7 05-26-2010 07:18 AM
Finding a real value for constant in Big O notation oulevon Programming 6 10-17-2003 05:04 PM

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

All times are GMT -5. The time now is 04:38 PM.

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