3 Replies - 1168 Views - Last Post: 10 March 2011 - 11:27 AM Rate Topic: -----

#1 Ap0C552   User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 325
  • Joined: 08-December 10

Insert sort infinite loop

Posted 10 March 2011 - 10:49 AM

I am required to implement an insert sort on an arraylist in an assignment I am working on.

For some reason I find it very confusing and it took me a while to wrap my head around it (I think because the text is crap).

Anyways for some reason my first for loop is in a infinite loop and the second for loop is not even running. I used a System.out.println to diagnose what was going on. I am getting confused here tbh :S

Any help would be so appreciated (obviously :P)

public static void sortEmployees(ArrayList<Employee>employees)
	{
        int count1;
        int count2;
        Employee element;

		for(count1=1;count1<employees.size();count1++)
		{
			System.out.println("11111111");
			element=employees.get(count1);
			for(count2=count1;count2>0&&employees.get(count2-1).compareTo(element)>0;count2--)
			{
				System.out.println("22222222");
				employees.add(count2,employees.get(count2-1));
			}
			employees.add(count2,element);

		}




	}



Is This A Good Question/Topic? 0
  • +

Replies To: Insert sort infinite loop

#2 SpeedisaVirus   User is offline

  • Baller

Reputation: 115
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Insert sort infinite loop

Posted 10 March 2011 - 11:02 AM

employees.add(count2,element);
after the second for loop is why its infinite. You add a new element every iteration so the size always increases at the same rate as the count1 increments. You need to remove the employee you plan to insert in it's proper order.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Insert sort infinite loop

Posted 10 March 2011 - 11:02 AM

Regarding your infinite loop - look at it. It's adding to the employee list each iteration... it will never have a max value!

You start at 1 and your max list is 2.
You cycle through iteration 1 and at the bottom it adds to the max so it's now 3.
You start iteration 2 and the max is 3... etc.. See?


Your second loop confuses me..

count2>0&&employees.get(count2-1).compareTo(element)>0


count2 is greater than (employees getting an element and comparing that element is greater than zero)?

Does that make sense when you say it out loud? It doesn't to me.

This post has been edited by modi123_1: 10 March 2011 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#4 Ap0C552   User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 325
  • Joined: 08-December 10

Re: Insert sort infinite loop

Posted 10 March 2011 - 11:27 AM

Ya I just figured this out.

I forgot that my arraylist would just keep growing and growing because the example I was looking at was an Array not an arraylist.

count2>0&&employees.get(count2-1).compareTo(element)>0



That line seems to make perfect sense to me.....

(While count2 is greater than zero AND Employee A compared to employee B returns a negative value)

(compareTo method returns a negative value when when first is less than last)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1