Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-03-2008, 11:38 PM
Member
 
Join Date: Nov 2008
Posts: 2
Rep Power: 0
Jordan is on a distinguished road
Default Please tell me I am not crazy... Time Complexity (Big-O) Question
Hi there,

New to the forums and I hope to help out as much as I can. I have lurked for a bit, however this is my first post. Anyways, here goes.

Is the time complexity of this code O(n^2)?

Code:
    public void placeLargest(int[] list, int N)
    {
    	
    	// N is how many of the largest items we want to find (N=3, find 3 largest)
    	int n = 0;
    	int largest, index;
    	int[] tempList = list;
    	
    	while (n < N)//Pass through the array N times
    	{
    		largest = tempList[0];
    		index = 0;
    		for (int i = 0; i < tempList.length; i++)
    		{
    			if (tempList[i] > largest)
    			{
    				largest = tempList[i];
    				index = i;	
    			}
    		}
    		tempList[index] = 0;
    		Nlargest.enqueue(largest);//Simlply place the value in a queue created earlier
    		n++;
    	}
    }
Thanks in advance for your help.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-04-2008, 12:53 AM
Senior Member
 
Join Date: Sep 2008
Posts: 564
Rep Power: 2
emceenugget is on a distinguished road
Default
I'm pretty sure that it's O(N*tempList.length)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-04-2008, 03:48 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Can you execute the code with worst case data and use a counter to find out?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question mark colon operator question orchid Advanced Java 8 11-27-2008 07:36 AM
Time Fireking New To Java 3 09-07-2008 02:30 AM
i click on it,then first time error page comes,second time click then product page co 82rathi.angara New To Java 21 08-01-2008 12:13 PM
Singleton considered stupid, Java and complexity fishtoprecords Forum Lobby 11 07-06-2008 04:38 AM
Errors driving me crazy! although compiles fine irishsea2828 New To Java 1 04-08-2008 04:23 PM


All times are GMT +2. The time now is 02:35 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org