简体   繁体   English

2D数组单行排序

[英]2D array single row sorting

I'm trying to implement the popular Huffman code algorithm with 2D arrays via java. 我正在尝试通过java用2D数组实现流行的霍夫曼编码算法。

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. 这个想法是创建这个2D数组,将输入的概率排序并插入到数组的第一行中,然后接下来的每一行中的第一个元素将是上一行中前2个元素的总和。

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. 不幸的是,我在2D数组中的各个行的排序中看到了一个奇怪的输出。 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 我正在使用的示例是这样的:从A到E的5个字母,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 如果代码按预期工作,则将看到2D数组的以下输出:第1行:0.1、0.15、0.2、0.25、0.3第2行:0.2、0.25、0.25、0.3、0.0第3行:0.25、0.3、0.45 ,0.0,0.0第4行:0.45,0.55,0.0,0.0,0.0第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 在此之后尝试打印数组将得到以下输出:第1行:0.1 0.15 0.2 0.25 0.3第2行:0.2 0.25 0.25 0.3 0.0第3行:0.25 0.3 0.3 0.0 0.0第4行: 0.3 0.3 0.0 0.0 0.0第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?! 突出显示的数字是不正确的,由于某种原因,在正确地对第二行进行了排序之后,第三行中的最后一个数字不是0.45而是0.3,并且0.3重复了其余的数字。出?

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. 出于某些荒谬的原因,我尝试用新数组替换copyedprobabilities数组,而不是覆盖旧数组。

Why that worked and overwriting copiedprobabilities didn't, I will never know... 为什么这样行得通而覆盖复制的概率却没有,我永远不会知道...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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