# Selection Sort on a 2D Array

• 10-19-2007
veronicak5678
Selection Sort on a 2D Array
Hello-
I need to do a selection sort on two different 2d arrays, sorting by the last column of one of them (thirdarray). The first column of thirdarray is the effective column size for the first (morearray) array. I am so confused. I cant find any examples to help me. Here is what I have so far:

Code:

```int selectionsort(int morearray[][5], int thirdarray[][3],int total){       int temp[5], index_of_largest;       for ( ;total>0;total--){           index_of_largest=0;           for (int index=1;index<=total;index++){               if (thirdarray[index][2]>thirdarray[index+1][2]){                 index_of_largest=index;               }               if (total!=index_of_largest){                 for (int f=0;f<thirdarray[index][0];f++){                 temp[index]=morearray[index][total];                 morearray[index][total]=morearray[index][index_of_largest];                 morearray[index][index_of_largest]=temp[index];                 }                 }           }       }       return(twoarray[total][4]);       return(morearray[total][5]);```
• 10-21-2007
iMalc
You need to do a selection sort huh? Must be homework...

You really can't find any examples of one of the most common sorting algorithms? Did you try using google?

Can you tell us what the problem is so that every person who reads this doesn't have to create a project and provide sample data and compile and run the program, please? It makes sense for you to do this once rather than expecting everyone else to do it.

Why would you think that you need to return anything? Is it supposed to sort the data and leave it sorted where it was, or is it supposed to make a sorted copy and leave the original arrays unchanged?
The second return can of course never be reached.

There are several buffer overruns in that code. E.g. An array of [5] elements has valid indexes of 0, 1, 2, 3 and 4, count those and you'll see there are 5 of them. So if you tried using index 5 that would be a sixth element, but you told it there were only 5.