title: 2D array single row sorting
body: <p>I'm trying to implement the popular Huffman code algorithm with 2D arrays via java.</p>
  
  <p>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.</p>
  
  <p>Then sort the current row before moving on to the next one.</p>
  
  <p>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.</p>
  
  <pre><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&lt;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&lt;numberofletters;x++){
          grandoverseer[x][0]=copiedprobabilities[x];
      }
  
      for(int x=0;x&lt;numberofletters;x++){
          System.out.println("Row "+grandoverseer[x][0]);
      }
  
  
      String [] lettercodes=new String[numberofletters];
      for(int x=0;x&lt;numberofletters;x++){
          lettercodes[x]="";
      }
  
  
  
     for(int i=1;i&lt;numberofletters;i++){
         grandoverseer[0][i]=grandoverseer[0][i-1]+grandoverseer[1][i-1];
        for(int j=1;j&lt;numberofletters;j++){
            if(j==4) break;
          grandoverseer[j][i]=grandoverseer[j+1][i-1];
        }
  
  
        for(int k=0;k&lt;numberofletters-i;k++){
            copiedprobabilities[k]=grandoverseer[k][i];
        }
  
        Sort(copiedprobabilities);
  
        for(int k=0;k&lt;numberofletters-i;k++){
            grandoverseer[k][i]=copiedprobabilities[k];
        }
  
  
     }
  
     for(int x=0;x&lt;numberofletters;x++){
         for(int y=0;y&lt;numberofletters;y++){
             System.out.println("Row "+(x+1)+": "+grandoverseer[y][x]);
         }
     }
  
   }
  
  
   public static void Sort(float [] array){
      int smallestposition;
      for( int i=0; i&lt;array.length;i++){
          smallestposition=i;
          for(int j=i+1; j&lt;array.length ;j++){
          if(array[j]&lt;array[smallestposition]){
              smallestposition=j;
          }
          }
          if(smallestposition!=i){
              float temp=array[i];
              array[i]=array[smallestposition];
              array[smallestposition]=temp;
          }
      }
  
    }
   }
  </code></pre>
  
  <p>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</p>
  
  <p>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</p>
  
  <p>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 <strong><em>0.3</em></strong> 0.0 0.0
  Row 4: <strong><em>0.3 0.3</em></strong> 0.0 0.0 0.0
  Row 5: <strong><em>0.3</em></strong> 0.0 0.0 0.0 0.0</p>
  
  <p>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?!</p>
  
  <p>Thank you in advance and sorry for any confusion!</p>