Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 7th, 2004, 09:27 AM
heatwave heatwave is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 6 heatwave User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Time Complexity and O()

hey

Im looking for some help with time complexities and the O() method.

Can anyone explain the basic theory behind working out the time complexity of an algorithm using the O() method?

To give you an idea of the level of explaination im looking for heres a example function

int arraySearcher(int[] array, int search)
{
boolean result = false;
int L = array.length;
for(int i = 0; i <L; i++)
if(array[i] == search)
result = true;
return result;
}

this is about the level im at, at working out time complexities. Im not looking for answers just a little help about how to work these things out.

its really puzzling for me

thanks in advance

andy

Last edited by heatwave : April 7th, 2004 at 01:58 PM.

Reply With Quote
  #2  
Old April 7th, 2004, 07:40 PM
rakeshkumar rakeshkumar is offline
Spiderman
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: USA
Posts: 15 rakeshkumar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Please refer to the first two chapters of any standard book on algorithms -

"Cormen, Rivest" .. " Sara Baase, Allen Van Gelder" .. Any of them should provide a good reading . 'Googling' does help.

Reply With Quote
  #3  
Old April 8th, 2004, 12:12 AM
codergeek42's Avatar
codergeek42 codergeek42 is offline
[Insert clever comment here.]
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jul 2003
Location: Anaheim, CA (USA)
Posts: 6,459 codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)  Folding Points: 39542 Folding Title: Starter FolderFolding Points: 39542 Folding Title: Starter Folder
Time spent in forums: 1 Month 1 Week 6 Days 21 h 41 m 54 sec
Reputation Power: 1231
Send a message via ICQ to codergeek42 Send a message via AIM to codergeek42 Send a message via Yahoo to codergeek42 Send a message via Google Talk to codergeek42
The code youi posted would have O(n) effiency. Here's why:

The item that is being searched might be the first item, the last item, or somewhere in the middle.
On average (i.e., the average of many function calls with different parameters, etc.), the function will have to search through half of the array to find the element. Reasonable, right? Okay. So, we say it is O(n) because the time that the search takes is direclty proportional to the amount of items in the array. For example, doubling the length of the array would, on average, double the time required to find an element. (Note: the proportionality constant of 1/2 is discarded, as are most proportionality constants when dealing with big-O notation, as it is called.) Hope this helps!

Another example: the binary search function.
This function is O(log n) because for each iteration, the amount of elements left to look at is halved. Thus, as n increase, the rate at which the time to search increases in direct proportion to the log (base 2) of n. Now, because of the change of base property, any logarithm can be written as a constant multiplied by a logarithm of a different base, [log base a of b = (log base x of b)/(log base x of a) for any x such that x>0 and x != 1] (I forget the exact formula, but it is something liike that) thus the base and constant are discarded, and we say that the binary search function is O(log n) becase the time to run a binary search an array of size/length n is directly porportional to the logarithm of n. Hope this helps! I'm sorry if I confused you, just ask if you need more help!
__________________
~~ Peter ~~
( My Blog: It's exactly like normal nerdiness, but completely different. ) :: ( Supporter of the EFF & FSF ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key )

Reply With Quote
  #4  
Old April 8th, 2004, 05:36 AM
heatwave heatwave is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 6 heatwave User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile

Hey

thanks for the explaination, its made things a lot clearer for me!

I tried google but got bombarded by alot of advanced math stuff but ill see if i can get a hold of those books from my libary - cant afford to buy them

once again thanks

andy

Reply With Quote
  #5  
Old April 8th, 2004, 12:05 PM
codergeek42's Avatar
codergeek42 codergeek42 is offline
[Insert clever comment here.]
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jul 2003
Location: Anaheim, CA (USA)
Posts: 6,459 codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)codergeek42 User rank is General 4th Grade (Above 100000 Reputation Level)  Folding Points: 39542 Folding Title: Starter FolderFolding Points: 39542 Folding Title: Starter Folder
Time spent in forums: 1 Month 1 Week 6 Days 21 h 41 m 54 sec
Reputation Power: 1231
Send a message via ICQ to codergeek42 Send a message via AIM to codergeek42 Send a message via Yahoo to codergeek42 Send a message via Google Talk to codergeek42
Glad I could help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Time Complexity and O()


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


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





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