C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 14th, 2006, 01:26 AM
jandali jandali is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 18 jandali User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 6 m 23 sec
Reputation Power: 0
Big O notation

Hi i have exam on monday, so i will be asking for help in doing question in which i am clear
Big O notation
Code:
Original - Code
    1) j =n;     do{     j = j/2     O(1)     while (j>0) Answer is (O(logn)) can someone help me explain how did they get this answer?


Code:
Original - Code
    question #2     j =n;     do{     j = j/2     for (k = 0; k<j; ++K) O(1)     O(1)     while (j>0) Answer is O(n) can someone help me explain how did they get this answer?


Code:
Original - Code
    question #3 int recursive(int n) { if (n<=1) return 1; else return (recursive(n-1) + recursive(n-1)); } Answer is O(n) can someone help me explain how did they get this answer? showing steps taken


Code:
Original - Code
    question #4 int QuickSort (int left, int right, int *a) { int p = SelectAndShuffle(left, right, a); if (p > left) QuickSort(left, p-1, a); if (p < right) QuickSort(p+1, right, a);} Answer is Tbest(n) = O(n log n), Tworst(n) = O(n2), Tavg(n) = O(n log n) can someone help me explain how did they get this answer? showing steps taken


AND LAST ONE I DON"T EVEN KNOW ANSWER
Code:
Original - Code
    Assume for simplicity that n is a power of two. int mystery (int k, int n) { int x; int y; if (n <= 1) { return(0); } else { for (int i=0 ; i < n ; ++i) { O(1) // to determine the value of k } x = mystery (k, n/2); // integer division y = mystery (n-k, n/2); // integer division return (x+y); } } Write the recurrence equation for T(n).

Reply With Quote
  #2  
Old April 14th, 2006, 05:33 AM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
Here's a quick guide (note that all answers are approximate, when we say O(logn) we don't necessarily mean LITTERALLY "logn"):
- O(n) represents complexity that is linearly related to n itself. For example, if you have n elements in a list and you have to delete them one by one. If you have n elements, you have n deletes. Or if you have to delete half the elements, then you have n/2 deletes. Regardless, the answer is expressed in terms of n to the first power times a constant (such as 2, 1/2, etc).
- O(logn) represents complexity that is approximately equal to n raised to a power less than one, or more accurately, a natural base (usually 2, in comp sci) raised to a power. This is basically saying that the time complexity is equal to some number exponentially less than n. For example, if you have a binary tree of data, finding an element of data does not require that you search every single element in the tree, you can use "divide and conquer" (if you will) to make your search faster and more direct. Thus, searching a 16 element database might only take 4 steps. If it were plain O(n) complexity, it would take about 8 on average. If you can express the time complexity as a function of n raised to a power less than 1, you probably have O(logn).

I can't recall just off hand exactily how to differenciate O(logn) from O(nlogn), though. IIRC, there isn't a perfect way to tell, you just have to estimate the time complexity and see if it appears to be somewhat linear (like O(n)), yet still logarithmic (O(logn)).

I hope some of that makes sense.

You might also be interested in reading this.
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old April 16th, 2006, 01:43 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
For first Ques: It is O(1) because it takes constant time to execute stmt. j = j/2.

and constant time complexity is O(1).

For secnd question it is O(n) becuse of for loop.
There are two operations : One is for loop and other is j = j/2 , but for loop time takes dominance so overall complexiy is O(n).

Reply With Quote
  #4  
Old April 16th, 2006, 01:53 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Q3.It is O(n) because each recursive call is taking n numbers into account. Had it been n/2 , complexity would have been O(logn).

Note that in the question Q3, value of n decreases constantly in each recursive call.


For Q4, quicksort takes variable time depending on nature of input being supplied.If it is presorted and you have to sort in reverse order , complexity is O(nlogn) . If it is sorted and you have to sort in same order , complexity is O(n^2).

For random ordered input complexity will again be O(nlogn).
you need to follow Quicksort algo.

One more point, it also depends on choice of your pivot.For example if you take median as pivot , quicksort is fastest.
If you take pivot as random , speed decreases by constant amount. and if you take extremes as pivots , speed is slowest by constant time.

Why? becuse if extremes are chosen as pivot, there is chance that one sub-portion has zero elements, becuse of which you have to start again.This ain't the case with pivot as median.

Reply With Quote
  #5  
Old April 16th, 2006, 01:57 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Last problem appears to be a O(logn) problem because recurrance equation is

T(n) = { O(1) , if n < 1
{ O(1) + 2T(n/2) , n > 1.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Big O notation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT