Take the 2-minute tour ×

What is the algorithm for a selection sort for a two dimensional array? I've looked and read up about 2D arrays everywhere, but I can't find anything that is simple and direct, so I've resorted to asking on a forum. (I can't seem to wrap my mind around how to change the sort of a 1D array to make it a 2D array!!)

For example, this:

name ----- crime ----- year

name1 ---- arson ----- 1996
name2 ---- theft ----- 2003
name3 ---- arson ----- 1976
name4 ---- theft ----- 2010

Becomes this:


name1 ---- arson ----- 1996
name3 ---- arson ----- 1976
name2 ---- theft ----- 2003
name4 ---- theft ----- 2010

Any help would be great! Thanks!


Here is the code I have for the selection sort. Most of them are organized properly, but in a couple of lines, I have "arson" where it doesn't belong and I'm not sure why that is. Here is my code:

        smallest = i;
        for(j = i; j < 10; j++){
            if(criminals[i][1].compareTo(criminals[j][1]) > 0){
                smallest = j;
        temp = criminals[i][1];
        criminals[i][1] = criminals[smallest][1];
        criminals[smallest][1] = temp;

        System.out.println(criminals[i][0] + " - " + criminals[i][1] + " - " + criminals [i][2]);

And this is my input and output:

Not sorted list:
Al Capone - arson - 2009
Slippery Sal - theft - 2001
Nada - arson - 1987
Slippery Sal - theft - 1999
Salma - assault - 2010
Scooby Doo - theft - 1998
Velma - assault - 1991
Daphne - arson - 1976
Fred - assault - 2003
Shaggy - arson - 2007
Sorted list (by crime):
Al Capone - arson - 2009
Slippery Sal - arson - 2001
Nada - arson - 1987
Slippery Sal - assault - 1999
Salma - arson - 2010
Scooby Doo - assault - 1998
Velma - assault - 1991
Daphne - theft - 1976
Fred - theft - 2003
Shaggy - theft - 2007
share|improve this question
Convert the array to a 1D array, sort that, and put the results back into the 2D array. –  arshajii Sep 20 '13 at 1:58
What do you mean by sorting a 2D array? In many languages, a 2D array is just a 1D array of arrays, and arrays are often comparable. Or are you thinking that the 2D array is a sequence of values just written in rows? Can you give an example? –  Ray Toal Sep 20 '13 at 1:58
@RayToal It is an array of arrays. I had to write a list of criminals (10 rows), and for each criminal I had to have a name, crime, year (3 columns). I want to sort the list by crimes and I have to use a selection sort, but I want the name of the criminal and year to stay with the crime. –  Sal Sep 20 '13 at 2:02
@RayToal See jade-cheng.com/hpu/2012-spring/csci-2912/… –  PM 77-1 Sep 20 '13 at 2:03
It would be better to create class Criminal (with all related information), unless it's a school exercise on 2D arrays. –  PM 77-1 Sep 20 '13 at 2:06

3 Answers 3

For your criminal thing, do this:

static final int SORT_BY_NAME = 0;
static final int SORT_BY_CRIME = 1;
static final int SORT_BY_YEAR = 2;

Assuming this :

Object[10][3] = {{"Name", "CRIME", "YEAR"},...};

Now to your sort algorithm:

void SelSort(Objects[][] criminals, int sortBy) 
    if (criminals == NULL || criminals.length == 0 || sortBy >= criminals[0].length || sortBy < 0)

    int i,j;
    Object min;

    for (i=0; i < criminals.length ; i++) {
       min = criminals[i][sortBy];
        for (j = i+1; j < criminals.length; j++){
            if (((criminals[j][sortBy].getClass()).cast(min)).compareTo(criminals[j][sortBy]) == 1){//Assuming you can compare them this way else make a method to compare
                Object tmp = criminals[j];
                criminals[j] = criminals[i];
                criminals[i] = tmp;

share|improve this answer
This was helpful! What are k and l, exactly? –  Sal Sep 20 '13 at 2:18
Unfortunately this is an incorrect interpretation of OP's question. @RayToal has the right idea. –  Zong Zheng Li Sep 20 '13 at 2:20
What i was doing, was traversing each element of the matrix, k and l where the indexs to traverse the whole matrix (2D array) to find the minimum. What i was doing, in small words, is think of the matrix(2D array) as 1 big 1D array and traverse it. Like 2x10 matrix has the following members: [a00 a01 a02 ... a09 -> a10 a11 a 12....a19] Now, k and l would let me, for each of those elements(lets say i was holding the current position in the matrix with i and j, to traverse the remaining elements to find the minimum and swap it. –  Sinn Sep 20 '13 at 2:35
This is correct. Although you might want to use lowercase for variable name (so it's criminals, not Criminals) and you can use obj.compareTo(obj1) == 1 instead of using obj > obj1 –  justhalf Sep 20 '13 at 3:27
Edited and corrected according to suggestions –  Sinn Sep 20 '13 at 3:30

Well first of all, to be picky, it looks like you have an array of Criminal objects, something like

class Criminal {
    private String name;
    private String crime;
    private int year;

Then you would have a 1D array of criminal objects. You can use the regular selection sort algorithm, but instead of comparing

criminals[i] < criminals[j]

you would compare

criminals[i].getCrime() < criminals[j].getCrime()

Now, let's be not picky and assume that you really do have a 2-D array of strings, so each row of the array is itself an array of strings. Then just do the selection sort but compare like this:

criminals[i][1] < criminals[j][1]

This works because

criminals[row][0]   is the name
criminals[row][1]   is the crime
criminals[row][2]   is the year

When you do the swap, entire rows will be swapped, and you'll have what you want.

share|improve this answer
This has been already discussed in comments. OP is doing school assignment on 2D arrays. –  PM 77-1 Sep 20 '13 at 2:21
True, but as a school assignment I think the OP is stuck with having to use the 2D arrays for which the [1] is likely to help. If someone else chimes in and finds this answer of no value beyond what is said in the comments, I'll happily delete it. –  Ray Toal Sep 20 '13 at 2:26
I used this and it worked! Except for one thing. One of the outputs is off. Most are organized perfectly, but then I have a random "arson" midst a bunch of "assault" listed crimes. I'll post my code. –  Sal Sep 20 '13 at 2:47
You can post the full code at ideone.com and provide a link. –  Ray Toal Sep 20 '13 at 2:54
@RayToal Here it is: ideone.com/yHKBk6 ! –  Sal Sep 20 '13 at 2:57

From your comment, the specified overall sort criteria is that: within each row, the elements must be sorted.

Run the Selection sort algorithm on each 1D array( of the 2D array) at a time.

share|improve this answer

Your Answer


