Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
BCM
User Panel

Site Notices
Posted: 4/28/2011 8:42:57 PM EDT
What the hell is wrong with this code:
   

public class PigeonSort{
public static void main(String a[]){
    int i;
    int array[] = {24,17,6,35,11,119,7,79};
    System.out.println("Values Before the sort:");
    for(i = 0; i < array.length; i++)
      System.out.print(array[i]+"  ");
    pigeon_Sort(array);
    System.out.println();
    System.out.print("\nValues after the sort:\n");
    for(i = 0; i <array.length; i++)
      System.out.print(array[i]+"  ");
}

public static int[] pigeon_Sort(int[] arr) {
 int i, j;
 int[] count = new int[arr.length];
 for(i = 0; i < arr.length; i++ ) {
 count[arr[i]]++;
}
   
 for(i=0,j=0; i < count.length; i++) {
 for(; count[i]>0; (count[i])––) {
   arr[j++] = i;
 }
}

 return arr;
}

}

Also, what does the part in red do *exactly*?
Link Posted: 4/29/2011 9:18:51 AM EDT
[#1]
bump for a second chance
Link Posted: 4/29/2011 9:20:59 AM EDT
[#2]
Quoted:
What the hell is wrong with this code:
   

public class PigeonSort{
public static void main(String a[]){
    int i;
    int array[] = {24,17,6,35,11,119,7,79};
    System.out.println("Values Before the sort:");
    for(i = 0; i < array.length; i++)
      System.out.print(array[i]+"  ");
    pigeon_Sort(array);
    System.out.println();
    System.out.print("\nValues after the sort:\n");
    for(i = 0; i <array.length; i++)
      System.out.print(array[i]+"  ");
}

public static int[] pigeon_Sort(int[] arr) {
 int i, j;
 int[] count = new int[arr.length];
 for(i = 0; i < arr.length; i++ ) {
 count[arr[i]]++;
}
   
 for(i=0,j=0; i < count.length; i++) {
 for(; count[i]>0; (count[i])––) {
   arr[j++] = i;
 }
}

 return arr;
}

}

Also, what does the part in red do *exactly*?


j++ means j = j + 1
arr[j++] means "Give me the value stored in arr[j] then increment j by 1."  Do not confuse this with arr[++j], which means "Increment j by 1 and then give me the value stored in arr[j]."
So yeah, like Allen said.  It stores the value of i in arr[j], then increments j by 1.
Link Posted: 4/29/2011 9:21:51 AM EDT
[#3]
The red part sets arr[j] = i, and then increments j by one.

As for "what the hell is wrong with it" uh... google 'pigeonhole sort' and then become enlightened.  Not doing your coursework for you!  
Link Posted: 4/29/2011 9:26:02 AM EDT
[#4]
Quoted:
The red part sets arr[j] = i, and then increments j by one.

As for "what the hell is wrong with it" uh... google 'pigeonhole sort' and then become enlightened.  Not doing your coursework for you!  

No shit...the code is already out there.

Link Posted: 4/29/2011 9:37:26 AM EDT
[#5]
If you don't know what a post-increment operator does, you have bigger problems than figuring out sort algorithms.
Link Posted: 4/29/2011 1:59:11 PM EDT
[#6]
I'm not asking anyone to do my homework for me.... I've already spent close to 5 hoursworking on this piece of code.  

I did pretty good in my first JAVA class, but my prof for this semesters class is VERY new to teaching (he JUST graduated...) and isn't very good at explaining things. We're basically learning this on our own.


Anyway..... the code posted is what I've put together based on examples from about 6 different JAVA help sites. It compiles OK, but throws an error at the call to pigeon_sort. I understand that it's trying to access an index out of bounds of the array, but don't see where...

Guys, ANY advice would be helpful.



ETA: Thank you, allenNH. neither of my professors EVER covered that particaular structuring of an incrementer and I couldn't find a good explanation online.
Link Posted: 4/29/2011 2:21:32 PM EDT
[#7]
Quoted:
I'm not asking anyone to do my homework for me.... I've already spent close to 5 hoursworking on this piece of code.  

I did pretty good in my first JAVA class, but my prof for this semesters class is VERY new to teaching (he JUST graduated...) and isn't very good at explaining things. We're basically learning this on our own.


Anyway..... the code posted is what I've put together based on examples from about 6 different JAVA help sites. It compiles OK, but throws an error at the call to pigeon_sort. I understand that it's trying to access an index out of bounds of the array, but don't see where...

Guys, ANY advice would be helpful.



ETA: Thank you, allenNH. neither of my professors EVER covered that particaular structuring of an incrementer and I couldn't find a good explanation online.


Ok, here's a bit more for you to chew on.  This:

public static int[] pigeon_Sort(int[] arr)
{
 int i, j;
 int[] count = new int[arr.length];
 for(i = 0; i < arr.length; i++ )
 {
   count[arr[i]]++;
 }



You are looping through the array 'arr', but you are accessing the array 'count' inside the loop.  This is the source of your problem.  

ETA:

int array[] = {24,17,6,35,11,119,7,79};
int[] count = new int[arr.length];


Here you are saying that count has 8 elements (0..7).  In the loop, you're trying to access count[arr[i]] the first time through.

count[arr[i]] = count[arr[0]] = count[24]
Link Posted: 4/29/2011 2:33:44 PM EDT
[#8]
shiverMeTimbers++;


Link Posted: 4/29/2011 6:39:58 PM EDT
[#9]
Thanks again. I think I should be able to correct that loop problem. I'll update ya when I get a chance to finish it (I have to work on a project for a different class tonight.).
Close Join Our Mail List to Stay Up To Date! Win a FREE Membership!

Sign up for the ARFCOM weekly newsletter and be entered to win a free ARFCOM membership. One new winner* is announced every week!

You will receive an email every Friday morning featuring the latest chatter from the hottest topics, breaking news surrounding legislation, as well as exclusive deals only available to ARFCOM email subscribers.


By signing up you agree to our User Agreement. *Must have a registered ARFCOM account to win.
Top Top