Pigeon hole sort infinite loop problem - help please

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4427 Views - Last Post: 14 May 2010 - 02:42 PM Rate Topic: -----

#16 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 294
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Pigeon hole sort infinite loop problem - help please

Posted 14 May 2010 - 02:42 PM

I'm sorry I haven't been home all day, but I'm very happy you managed to figure out a way to make it work. Here was my solution

//created a int rowCounter to be used
static int rowCount = 0;

public static void mailSort(int[] array)
        {
                int low = array[0];
                int high = array[0];
                int count = 0;
                
                //added this
                System.out.println("Row: " + rowCount);
                rowCount++;
                
                for(int i=0; i<array.length; i++)
                {
                                int val = array[i];
                                if(val < low)
                                                {low = val;}
                                if(val > high)
                                                {high = val;}
                }
                int range = high - low +1; //Because we need to account for array index 0 later.
                int[] holes = new int[range];
                for(int i=0; i<array.length; i++)
        {
                        int value = array[i] - low;             
                        holes[value]++;
        }
                for(int i=0; i<holes.length; i++)
                {
                                //System.out.println(holes[i]); <-- took this out so it would stop printing all those zeros
                                if(holes[i] != 0)//changed this to "!=" so as long as it wasn't zero, it would print out. 
                                {
                                                for(int j=0; j<holes[i]; j++)
                                                {
                                                                array[count] = i + low;
                                                                System.out.println("The number " + (i+low) + " entered into position " + count);
                                                                count++;
                                                }       
                                }
                }
                return;
        }




Great job to you for taking initiative to figure it out. Those headaches are the best because they teach you the most.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2