Results 1 to 3 of 3
  1. #1
    Jordan is offline Member
    Join Date
    Nov 2008
    Rep Power

    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)?

    Java 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
    Thanks in advance for your help.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Rep Power


    I'm pretty sure that it's O(N*tempList.length)

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    Can you execute the code with worst case data and use a counter to find out?

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Time
    By Fireking in forum New To Java
    Replies: 3
    Last Post: 09-07-2008, 01:30 AM
  3. Replies: 21
    Last Post: 08-01-2008, 11:13 AM
  4. Singleton considered stupid, Java and complexity
    By fishtoprecords in forum Forum Lobby
    Replies: 11
    Last Post: 07-06-2008, 03:38 AM
  5. Errors driving me crazy! although compiles fine
    By irishsea2828 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts