1

I have a test tomorrow where we write a code based on what is asked. I need some explanation on how to sort a 2D array in increasing order. I can do this for a 1D array but I'm not sure if the same code will work for the 2D. Can you just explain how to implement this for a 2D array in your own way, I don't want you to think this is for homework, I just need to know how to do this for a test tomorrow. Thanks

for (i = 0; i < a.length - 1; i++) {
 for (j = i+1; j < a[0].length; j++) {
      if (a[i] < a[j]) {
           int temp = a[i];
           a[i] = a[j];
           a[j] = temp;
           System.out.print(temp);
      }
 }

}

user3033712
  • 23
  • 1
  • 1
  • 3
  • 2
    Can you describe what "in order" means for a 2 dim array? Is there one or more columns that are used to determine a sorted 2 dim array? – NormR Nov 26 '13 at 01:07
  • may be this helps http://stackoverflow.com/questions/20931669/sort-a-2d-array-in-c-using-built-in-functionsor-any-other-method – prime Jan 25 '17 at 14:12
  • This has an answer to this. http://stackoverflow.com/questions/18705127/how-to-sort-a-2d-array/41853842#41853842 – prime Jan 25 '17 at 14:17

4 Answers4

1

It seems that you want to sort each line of your matrix. You could simply go over each line and sort using off-the-shelf java method, given a is a two dimensional array :

for (i = 0; i < a.length; i++) {
  Arrays.sort(a[i]);
}

Anyway your question is not crystal clear to me and I join @Normr on its comment.

Julien
  • 1,302
  • 10
  • 23
1

Make the 2D array into a separate simple (1D) array (STEP 1).
Then use the Arrays.sort() method to sort the simple array (STEP 2).
Then set each space of the 2D array to be the number of columns across (X-coordinate where the space will be changed) multiplied by the number of spaces per row in the 2D array. Then add the row number (the Y-coordinate where the space will be changed) and you will have the index of the simple string that you need (STEP 3).

My print method is at bottom.

public static void sort2DArray(int[][] arrayName) 
{
    int[] simpleArray = new int[(arrayName[0].length)*(arrayName.length)];
    for(int r = 0; r < arrayName.length; r++) //CYCLE THROUGH ROWS (Y VALUES)
    {
        for(int c = 0; c < arrayName[0].length; c++) //CYCLE THROUGH COLUMNS (X VALUES)
        {

            simpleArray[arrayName[0].length*r+c] = arrayName[r][c]; //*STEP 1*
        }
    }
    Arrays.sort(simpleArray); //*STEP 2*

    for(int r = 0; r < arrayName.length; r++) //CYCLE THROUGH ROWS (Y VALUES)
    {
        for(int c = 0; c < arrayName[0].length; c++) //CYCLE THROUGH COLUMNS (X VALUES)
        {
            arrayName[r][c] = ( simpleArray[(r * arrayName[0].length) + c ] ); //*STEP 3*
        }
    }
}

public static void print2DArrayAsTable(int[][] arrayName)  //METHOD TO PRINT A 2D ARRAY AS A TABLE
{
    for(int c = 0; c < arrayName.length; c++) //CYCLE THROUGH COLUMNS (X VALUES)
    {
        for(int r = 0; r < arrayName[0].length; r++) //CYCLE THROUGH ROWS (Y VALUES)
        {
            p.o(arrayName[c][r] + " "); //PRINT INDIVIDUAL ARRAY SPACE VALUE
        }
        p.l();
    }
}
GJW
  • 83
  • 1
  • 8
0

One way to to it is basically to implement a selection sort (so you iterate through elements in order in which you want elements to be sorted, and for each element you search through the rest of your table - EXCLUDING the elements before your current element - select the smallest element and swap it with your current element). This would be O(n^2) where n is the total size of your array.

Another way is to copy your elements to a 1d array, use any correct algorithm to sort it and then copy your ordered elements to correct places in your 2d array (so that it is sorted in the way you want it). With a proper data set this could be O(n) as it takes O(n) for copying and could take O(n) for Counting Sort or Bucket Sort (if they would be appropriate for your set). In the worst case this would be O(nlogn), as you could use MergeSort, HeapSort, QuickSort or any comparison-based sorting algorithm.

3yakuya
  • 2,622
  • 4
  • 25
  • 40
0

according to your description,i think you could resolve the problem like this.

first,loop the 2D array,and put every element into a arrayList. then, use the exists method Collections.sort(List paramList) for sorting the arrayList.

you will get a sorted list in the end.

ice
  • 124
  • 4