简体   繁体   中英

2D array single row sorting

I'm trying to implement the popular Huffman code algorithm with 2D arrays via java.

The idea is to create this 2D array, have the entered probabilities sorted and inserted into the first row in the array, then the first element in each following row will be the sum of the first 2 elements in the previous row.

Then sort the current row before moving on to the next one.

Unfortunately there's a weird output that I'm seeing with the sorting of the individual rows in the 2D array. Here's the code.

   import java.util.Scanner;
   public class SolaihtheHuffman {
    public static void main(String[] args) {
     Scanner input=new Scanner(System.in);
     System.out.println("Please enter the number of letters.");
     int numberofletters=input.nextInt();
     float [][] grandoverseer=new float[numberofletters][numberofletters];

     float [] probabilities = new float [numberofletters];

     System.out.println("Please enter each letter's probability.");
     for(int x=0;x<numberofletters;x++){
        probabilities[x]=input.nextFloat();
     }




    float [] copiedprobabilities=new float[numberofletters];
    System.arraycopy(probabilities, 0, copiedprobabilities, 0, numberofletters);



    Sort(copiedprobabilities);   //WORKS PROPERLY


    for(int x=0;x<numberofletters;x++){
        grandoverseer[x][0]=copiedprobabilities[x];
    }

    for(int x=0;x<numberofletters;x++){
        System.out.println("Row "+grandoverseer[x][0]);
    }


    String [] lettercodes=new String[numberofletters];
    for(int x=0;x<numberofletters;x++){
        lettercodes[x]="";
    }



   for(int i=1;i<numberofletters;i++){
       grandoverseer[0][i]=grandoverseer[0][i-1]+grandoverseer[1][i-1];
      for(int j=1;j<numberofletters;j++){
          if(j==4) break;
        grandoverseer[j][i]=grandoverseer[j+1][i-1];
      }


      for(int k=0;k<numberofletters-i;k++){
          copiedprobabilities[k]=grandoverseer[k][i];
      }

      Sort(copiedprobabilities);

      for(int k=0;k<numberofletters-i;k++){
          grandoverseer[k][i]=copiedprobabilities[k];
      }


   }

   for(int x=0;x<numberofletters;x++){
       for(int y=0;y<numberofletters;y++){
           System.out.println("Row "+(x+1)+": "+grandoverseer[y][x]);
       }
   }

 }


 public static void Sort(float [] array){
    int smallestposition;
    for( int i=0; i<array.length;i++){
        smallestposition=i;
        for(int j=i+1; j<array.length ;j++){
        if(array[j]<array[smallestposition]){
            smallestposition=j;
        }
        }
        if(smallestposition!=i){
            float temp=array[i];
            array[i]=array[smallestposition];
            array[smallestposition]=temp;
        }
    }

  }
 }

The example i'm working with is this : 5 letters from A to E, A : 0.3 , B : 0.1, C : 0.2, D: 0.15, E: 0.25

If the code worked as intended I'd see the following output of the 2D array: Row 1 : 0.1, 0.15, 0.2, 0.25, 0.3 Row 2 : 0.2, 0.25, 0.25, 0.3, 0.0 Row 3 : 0.25, 0.3, 0.45, 0.0, 0.0 Row 4 : 0.45, 0.55, 0.0, 0.0, 0.0 Row 5 : 1.0, 0.0, 0.0, 0.0, 0.0

Trying to print the array after this gives me the following output : Row 1: 0.1 0.15 0.2 0.25 0.3 Row 2: 0.2 0.25 0.25 0.3 0.0 Row 3: 0.25 0.3 0.3 0.0 0.0 Row 4: 0.3 0.3 0.0 0.0 0.0 Row 5: 0.3 0.0 0.0 0.0 0.0

The highlighted numbers are incorrect, for some reason after sorting the 2nd row correctly, the last number in the 3rd row is not 0.45 but 0.3, and that 0.3 is duplicated in place of the rest of the numbers.. Anyone wanna help me figure this out?!

Thank you in advance and sorry for any confusion!

For some ridiculous reason that is beyond me I tried replacing the copiedprobabilities array with a new array instead of overwriting the old one.

Why that worked and overwriting copiedprobabilities didn't, I will never know...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM