Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column sort an array?

I want to column-sort a multi-dimensional array. I have the code setup, but it's not displaying the right results...

Example before sorting:

6.0 4.0 2.0

4.0 2.0 4.0

1.0 3.0 1.0

Example after sorting:

1.0 2.0 1.0

4.0 3.0 2.0

6.0 4.0 4.0

This is my code:

  import java.util.Scanner;

    public class ColumnSorting
    {
   public static void main(String [] args)
   {
     run();
   }
   public static void run()
   {
      Scanner input = new Scanner(System.in);
      System.out.print("Please enter the values of your 3x3 matrix: ");
      double[][] matrix = new double[3][3];
      for (int i = 0; i < matrix.length; i++)
      {
         for (int k = 0; k < matrix[i].length; k++)
         {
            matrix[i][k] = input.nextDouble();
         }
      }
      printArrays(matrix);

    }
   public static void printArrays(double[][] matrix)
   {
   System.out.println("Before sorting: ");

       for (int i = 0; i < matrix.length; i++)
      {
        for (int k = 0; k <matrix[i].length; k++)
         {
            System.out.print(matrix[i][k] + " ");
         }
         System.out.println();
      }
    double[][] newMatrix = sortColumns(matrix);
     System.out.println();
    System.out.println("After sorting: ");

    for (int i = 0; i < newMatrix.length; i++)
      {
         for (int j =0; j < newMatrix[i].length; j++)
         {
            System.out.print(newMatrix[i][j] + " ");
         }
         System.out.println();
      }  

   }
   public static double[][] sortColumns(double[][] m)
   {
      double min;
      double temp;
      for (int i = 0; i < 3; i++)
      {
         min = m[0][i];
         for (int k = 0; k < m.length; k++)
         {

            if (min > m[k][i])
            {
               temp = min;
               m[0][i] = m[k][i];
               m[k][i] = temp;
            }
         }
      }
      return m;

   }
}

This is what i'm getting:

1.0 3.0 1.0

6.0 4.0 4.0

6.0 4.0 2.0

Please tell me what i'm doing wrong!

Thanks!

like image 943
user2896120 Avatar asked Apr 27 '15 07:04

user2896120


People also ask

How do you sort an array by a column?

NumPy arrays can be sorted by a single column, row, or by multiple columns or rows using the argsort() function. The argsort function returns a list of indices that will sort the values in an array in ascending value.

How do I sort an array in Excel?

The syntax for the new SORT function is =SORT(array, [sort_index], [sort_order], [by_column]). The first argument identifies the array to be sorted. All the other arguments are optional. The second argument determines which column the array will be sorted by.

How can we sort a 2D NumPy array based on two columns?

Sorting 2D Numpy Array by column at index 1 Select the column at index 1 from 2D numpy array i.e. It returns the values at 2nd column i.e. column at index position 1 i.e. Now get the array of indices that sort this column i.e. It returns the index positions that can sort the above column i.e.


2 Answers

Your sort algorithm is not sorting - it is doing something strange.

Here's a bubble-sort equivalent for demonstration.

public static double[][] sortColumns(double[][] m) {
    for (int col = 0; col < m[0].length; col++) {
        // Have we swapped one on this pass?
        boolean swapped;
        do {
            swapped = false;
            for (int row = 0; row < m.length - 1; row++) {
                // Should these be swapped?
                if (m[row][col] > m[row + 1][col]) {
                    // Swap this one with the next.
                    double temp = m[row][col];
                    m[row][col] = m[row + 1][col];
                    m[row + 1][col] = temp;
                    // We swapped! Remember to run through again.
                    swapped = true;
                }
            }
        } while (swapped);
    }
    return m;

}

Although a better option would be to copy each column out to a separate array, sort it and put it back in - at least that way you would not be needing to implement your own sorting algorithm.

public static double[][] sortColumnsProperly(double[][] m) {
    // Sort each colum.
    for (int col = 0; col < m[0].length; col++) {
        // Pull the column out.
        double[] thisCol = new double[m.length];
        for (int i = 0; i < m.length; i++) {
            thisCol[i] = m[i][col];
        }
        // Sort it.
        Arrays.sort(thisCol);
        // Push it back in.
        for (int i = 0; i < m.length; i++) {
            m[i][col] = thisCol[i];
        }
    }
    return m;

}
like image 102
OldCurmudgeon Avatar answered Nov 15 '22 04:11

OldCurmudgeon


I knocked up an example of yout problem, hope it helps.

Code

public class Demo {

    public static void main(String[] args) {

        double[][] m = new double[4][4];

        m[0][0] = 6.4;
        m[0][1] = 4.0;
        m[0][2] = 2.0;

        m[1][0] = 4.0;
        m[1][1] = 2.0;
        m[1][2] = 4.0;

        m[2][0] = 1.0;
        m[2][1] = 3.0;
        m[2][2] = 1.0;

        System.out.println("---Before sort---");
        printSort(m);

        double[][] x = matrixColumnSort(m);

        System.out.println("---After sort---");
        printSort(x);
    }

    public static double[][] matrixColumnSort(double[][] m) {

        ArrayList<Double> arrayList = new ArrayList<Double>();
        for (int i = 0; i < m.length - 1; i++) {
            for (int j = 0; j < m.length - 1; j++) {
                arrayList.add(m[j][i]);
            }
            Collections.sort(arrayList);
            for (int j = 0; j < m.length - 1; j++) {
                m[j][i] = arrayList.get(j);
            }
            arrayList.clear();
        }
        return m;
    }

    public static void printSort(double[][] m) {
        for (int i = 0; i < m.length - 1; i++) {
            for (int j = 0; j < m.length - 1; j++) {
                System.out.print(" " + m[i][j] + " ");
            }
            System.out.println("");
        }
    }
}

Console Output

---Before sort---
 6.4  4.0  2.0 
 4.0  2.0  4.0 
 1.0  3.0  1.0 
---After sort---
 1.0  2.0  1.0 
 4.0  3.0  2.0 
 6.4  4.0  4.0 
like image 31
Kevvvvyp Avatar answered Nov 15 '22 05:11

Kevvvvyp